Visual Studio Developer Center > Visual Studio Team System Forums > Visual Studio Code Analysis and Code Metrics > (NullReferenceException) - Object reference not set to an instance of an object.

Answered (NullReferenceException) - Object reference not set to an instance of an object.

  • Tuesday, July 22, 2008 9:04 PM
     
     
    Ofcourse I have read plenty of other topics about people having this kind of problem but they all didnt seem to work for me. A common cause was when a object was not Dim'ed As New. Or when object was Nothing/null.

    However my code is nothing like this I believe. Here's my code that causes the exception:
    Code Snippet

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim fs2 As New FileStream("names.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
            Dim sr2 As New StreamReader(fs2)
            Dim NameIsPresent As Boolean = False
            Dim NoLine As String = ""
            Dim PrevLine As String = ""
            Dim CurLine As String = ""

            ListBox1.Items.Clear()

            Do While sr2.Peek <> -1
                PrevLine = sr2.ReadLine()
                CurLine = sr2.ReadLine()
                NoLine = sr2.ReadLine()
                If CurLine.StartsWith(TextBox1.Text) Then ListBox1.Items.Add(PrevLine)
                If CurLine = TextBox1.Text And PrevLine = TextBox2.Text Then
                    NickIsPresent = True
                End If
            Loop

            If NameIsPresent = False Then
                Dim sw As StreamWriter
                sr2.Close()
                sw = File.AppendText("Names.txt")
                sw.Write(TextBox2.Text)
                sw.Write(TextBox1.Text)
                sw.Write("")
                sw.Flush()
            End If
        End Sub


    What this code should do is read a textfile to check if the name(textbox2.text) is present in it. Then if it is not, it should add the name and address of the person to the text file. Also the code should place names with same address in listbox1.
    Sounds easy but a keep getting the error. Could somebody provide me with a working code sample because this "NullReferenceException -  Object reference not set to an instance of an object" is really confusing me.

    Thanks in advance!

Answers

  • Wednesday, July 23, 2008 7:16 AM
     
     Answered

    Not entirely sure its CA rule related but my guess is .....

     

    That you run out of data during the reads in your while loop, and when you run out of data the ReadLine calls you make return null

     

     Do While sr2.Peek <> -1                                     
                PrevLine = sr2.ReadLine()                     <--- If this line reaches the end of the file
                CurLine = sr2.ReadLine()                      <--- then this is null
                NoLine = sr2.ReadLine()                       <---- as is this
                If CurLine.StartsWith(TextBox1.Text) Then ListBox1.Items.Add(PrevLine

                     ^

    Which means CurLine is null here.

     

    Then when you do CurLine.StartsWith the error occurs because CurLine is null so you cant check what it startswith.

     

    The Documentation for ReadLine is here:  http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline.aspx