Thanks to a friend I was able to code a sub that opens a StreamReader and Writer. The Reader reads a *.txt file and the Writer modifies it line by line as it's entered into the memory then writes it to a temporary file which is then copied over the file that the Reader read.

The problem is that the StreamReader and Writer seem to fail to close.

At first when I tested it I would get an error stating:

Object reference not set to an instance of an object

I put breakpoints in the sub to figure out where it's erroring out and it's erroring out when it does sr.close() and sw.close() (Reader/Writer.close())

Here's the code for the sub. Maybe someone can tell me what I did wrong.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If pDebugMessages = True Then MsgBox("Stream Reader/Writer initialized.", MsgBoxStyle.OkOnly, "Debug")

        Try
            Using sr As StreamReader = New StreamReader(ToolStripStatusLabel4.Text)
                Using sw As StreamWriter = New StreamWriter(Environment.GetEnvironmentVariable("PUBLIC") & "\" & "TextReplacer\trtemp.txt")

                    Dim line As String

                    Do
                        line = sr.ReadLine()
                        If o1.Text = "Replace" Then
                            line.Replace(str1.Text, a1.Text)
                        End If
                        sw.WriteLine(line)
                    Loop Until line Is Nothing
                    sr.Close()
                    sw.Close()
                End Using
            End Using
        Catch ex As Exception
            If pDebugMessages = True Then MsgBox("An error has occured attempting writing.", MsgBoxStyle.OkOnly, "Debug")
            MsgBox(ex.Message)
        End Try

        Dim orig As String = Environment.GetEnvironmentVariable("PUBLIC") & "\" & "TextReplacer\trtemp.txt"
        Dim temp As String = ToolStripStatusLabel4.Text

        Try
            File.Delete(temp)
            File.Copy(orig, temp)

        Catch ex As Exception
            If pDebugMessages = True Then MsgBox("An error has occured while attempting to write.", MsgBoxStyle.OkOnly, "Debug")
            MsgBox(ex.Message)
        End Try

        Try
            Dim r As IO.StreamReader
            txtfile.Items.Clear()
            r = New IO.StreamReader(ToolStripStatusLabel4.Text)
            While (r.Peek() > -1)
                txtfile.Items.Add(r.ReadLine)
            End While
            r.Close()
            If pDebugMessages = True Then MsgBox("File reloaded succesfully.", MsgBoxStyle.OkOnly, "Debug")
        Catch ex As Exception
            MsgBox(ex.Message)
            If pDebugMessages = True Then MsgBox("An error has occured while attempting to reload the file.", MsgBoxStyle.OkOnly, "Debug")
        End Try
    End Sub

I've been stuck trying to figure out why it's erroring out for days, but I haven't figured it out.

Thanks.

Edited 7 Years Ago by Darkicon: n/a

You do not need to explicitly close the reader & writer in this situation. The use of the "USING" block will open, close & dispose of the object when it exits the USING block.

Thanks, I tried that, but I still get the same error, this time on the first End Using.

I'm guessing the error is being caused by "Loop Until line Is Nothing"?

Edited 7 Years Ago by Darkicon: n/a

This article has been dead for over six months. Start a new discussion instead.