Results 1 to 4 of 4

Thread: PLEASE HELP: Visual Basic Error Object reference not set to an instance of an object?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Location
    United States
    Posts
    29

    PLEASE HELP: Visual Basic Error Object reference not set to an instance of an object?

    I have this visual basic error and i was wondering if anyone could help.

    Code:
    Code:
    Private Sub SearchToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchToolStripMenuItem.Click
    
    
    
    If My.Computer.FileSystem.DirectoryExists("… Then
            If My.Computer.FileSystem.FileExists("conta… Then
                 If My.Computer.FileSystem.FileExists("conta… Then
                      Dim a As New System.IO.StreamReader("contacts/list.da…
                      Dim b As New System.IO.StreamReader("contacts/elist.d…
    
                      Dim c As String = "HOLDER"
                      Dim x As String = "HOLDER"
    
    
                    Do While Not c = ""
                      c = a.ReadLine
                      x = b.ReadLine
                      If Not c = "" Or x = "" Then
                              Dim search As String = search_t.Text
                              Dim l As Boolean = c.Contains(search)
                              Dim m As Boolean = x.Contains(search)
                              If l = True Or m = True Then
                                      Dim d As New ListViewItem
                                      d.Text = c
                                      d.SubItems.Add(x)
                                      ListView1.Items.Add(d)
                              End If
                      End If
    
    
                 Loop
                   a.Close()
                   b.Close()
                 End If
           End If
    End If
    End Sub
    I get the Error on this line:
    Code:
    Dim l As Boolean = c.Contains(search)
    The error is:
    Object reference not set to an instance of an object.

    PLEASE HELP! THANK YOU!
    If I fail... You can prolly go anywhere else and get teh right answer

    Please Visit my site:
    http://www.fiscalleti.com/

  2. #2
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    Re: PLEASE HELP: Visual Basic Error Object reference not set to an instance of an obj

    one possible problem is: a & b files do not have the same # of lines. so, c & x, may return nothing. so, whne you do c.contains, it has error.

    use try catch to see the ex message.

    bear

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: PLEASE HELP: Visual Basic Error Object reference not set to an instance of an obj

    That error can always be examined the same way. When you get the error, examine each object in the line (either hover over them, or highlight the object and press Shift+F9). One of them is Nothing, and figuring out which object is the first thing you need to do.

    In the case of your line, there are three objects: l, c, and search.

    If can't be l based on that line. You are setting it to something with that line, so even if it was Nothing, it wouldn't make a bit of difference to that line.

    If search was Nothing, the error (probably) wouldn't appear to be that line, though that is not always the case.

    That leaves c. If c is Nothing, then you can't call a method on it, so you can't call Contains without getting the exact error that you have gotten. Therefore, it is very likely that c is the problem, but you should confirm that by seeing what c holds when the error occurs.

    Now that you have good reason to suspect that the problem is that c is Nothing, the next question is WHY is it nothing? That's not so easy to answer. c is being set by the line:

    c = a.ReadLine

    Therefore, for c to be Nothing, a.ReadLine would have to return Nothing. It seems likely that you were not expecting that. Instead, it appears that you were expecting that a.ReadLine might return an empty string (""), but not Nothing. However, it looks like it IS returning Nothing. If that is ok with you, then all you would need to do is to change this line:

    If Not c = "" Or x = "" Then

    To this:

    If c IsNot Nothing Andalso c <> "" AndAlso x IsNot Nothing AndAlso x <> "" Then


    Take a careful look at that line, though, because I might be misunderstanding what your original If statement was doing. I figured you meant that you wanted to perform the operation if neither c nor x was an empty string. I changed that to check that neither c nor x was either Nothing or an empty string. (and note that I used AndAlso, which you should almost always use rather than And, just as you should almost always use OrElse rather than Or, unless you have a specific need for the shorter versions. Look them up if you are curious as to why.)

    You will also want to change you Do Loop condition in a similar way to loop as long as c is not an empty string and is not Nothing.
    My usual boring signature: Nothing

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: PLEASE HELP: Visual Basic Error Object reference not set to an instance of an obj

    Quote Originally Posted by FlyingBear View Post
    use try catch to see the ex message.

    bear
    Actually, if he does that, he will see the same message that he is already seeing, he just won't know which line raised it.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width