Results 1 to 11 of 11

Thread: Object reference not set to an instance of an object.

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    35

    Object reference not set to an instance of an object.

    I got this error from this code:

    ERROR:
    An unhandled exception of type 'System.NullReferenceException' occurred in Lap Time Analyser.exe

    Additional information: Object reference not set to an instance of an object.

    CODE:
    VB Code:
    1. Private Sub btnplusminus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnplusminus.Click
    2.         Dim laptime() As String
    3.         Dim i, num As Integer
    4.         Dim sr As IO.StreamReader = IO.File.OpenText(path)
    5.         Do While (sr.Peek <> -1)
    6.             sr.ReadLine()
    7.             num += 1
    8.         Loop
    9.         sr.Close()
    10.         Dim srr As IO.StreamReader = IO.File.OpenText(path)
    11.         For i = 1 To num
    12.             laptime(i) = srr.ReadLine
    13.         Next
    14.         srr.Close()
    15.     End Sub

    Im thinking its the laptime(i) that is screwing up because that is where it stops in the debugging tool. Help PLZ

    My file looks like :

    Alain
    Ste-Julie
    03
    02
    21
    4/18/2006 5:25:14 PM

  2. #2
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Object reference not set to an instance of an object.

    You need to put a new before io.streamreader to actually create the streamreader

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    35

    Re: Object reference not set to an instance of an object.

    How would I do that? Whats the code? I dont know where exactly to put it.

  4. #4
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Object reference not set to an instance of an object.

    VB Code:
    1. Dim sr As New IO.StreamReader(path)
    Like that

  5. #5
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Object reference not set to an instance of an object.

    Quote Originally Posted by alain250sxf
    Whats the code?
    Quote Originally Posted by |2eM!x
    new
    Quote Originally Posted by alain250sxf
    I dont know where exactly to put it.
    Quote Originally Posted by |2eM!x
    put a new before io.streamreader
    |2eM!x already answered your questions
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    35

    Re: Object reference not set to an instance of an object.

    That didnt fix the bug.
    It still says:
    An unhandled exception of type 'System.NullReferenceException' occurred in Lap Time Analyser.exe

    Additional information: Object reference not set to an instance of an object.

    And if I put the array (laptime()) as double is says :
    An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

    Additional information: Cast from string "Alain " to type 'Double' is not valid.

    It has something to do with the array. I just dont know what.

    My code now looks like :
    VB Code:
    1. Private Sub btnplusminus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnplusminus.Click
    2.         Dim laptime() As Double
    3.         Dim i, num As Integer
    4.         Dim sr As IO.StreamReader = IO.File.OpenText(path)
    5.         Do While (sr.Peek <> -1)
    6.             sr.ReadLine()
    7.             num += 1
    8.         Loop
    9.         sr.Close()
    10.         PlusMinus(laptime, i, num)
    11.  
    12.     End Sub
    13.     Private Sub PlusMinus(ByRef laptime() As Double, ByRef i As Integer, ByRef num As Integer)
    14.  
    15.         Dim sr As New IO.StreamReader(path)
    16.         For i = 1 To num
    17.             laptime(i) = (sr.ReadLine)
    18.         Next
    19.         sr.Close()
    20.  
    21.     End Sub
    Last edited by alain250sxf; Apr 19th, 2006 at 11:09 PM.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,448

    Re: Object reference not set to an instance of an object.

    Actually, I think you'll find that it has nothing to do with the StreamReader. File.OpenText returns a StreamReader object, so while Remix's code is not incorrect, it is not necessary in this case.

    The problem is the fact that you haven't created an array. This line:
    VB Code:
    1. Dim laptime() As String
    does NOT create an array object. It declares an array variable, but that variable does not refer to an object. You have to specify the upper bound to actually create an array object. Arrays are fixed-size so one cannot possibly be created if you haven't specified that size in the first place. That all means that this line:
    VB Code:
    1. laptime(i) = srr.ReadLine
    is attempting to set an element that doesn't exist in an array that doesn't exist. You're reading the entire file twice. The first time you're getting the number of lines so you could use that to create the array, but that is incredibly inefficient. What if you have thousands of lines? Do you really want to read them all just to get the number and then read them all again? If you want to use an array-like object that can be resized dynamically then you should use a collection. Because you are storing strings the obvious choice is the StringCollection. You could change this:
    VB Code:
    1. Dim laptime() As String
    2.         Dim i, num As Integer
    3.         Dim sr As IO.StreamReader = IO.File.OpenText(path)
    4.         Do While (sr.Peek <> -1)
    5.             sr.ReadLine()
    6.             num += 1
    7.         Loop
    8.         sr.Close()
    9.         Dim srr As IO.StreamReader = IO.File.OpenText(path)
    10.         For i = 1 To num
    11.             laptime(i) = srr.ReadLine
    12.         Next
    to this:
    VB Code:
    1. Dim lapTimes As New Specialized.StringCollection
    2.  
    3.         Dim sr As IO.StreamReader = IO.File.OpenText(path)
    4.  
    5.         Do Until sr.Peek = -1
    6.             lapTimes.Add(sr.ReadLine())
    7.         Loop
    8.  
    9.         sr.Close()
    There are even easier ways to go about this though. You haven't specified what version of VB you're using (*sigh*) so I'll give you code for both.

    2003:
    VB Code:
    1. Dim sr As IO.StreamReader = IO.File.OpenText(path)
    2.         Dim lapTimes As String() = System.Text.RegularExpressions.Regex.Split(sr.ReadToEnd(), Environment.NewLine)
    3.  
    4.         sr.Close()
    2005:
    VB Code:
    1. Dim lapTimes As String() = My.Computer.FileSystem.ReadAllText(path).Split(New String() {Environment.NewLine}, StringSplitOptions.None)
    There you go. One line in VB 2005 and three lines in earlier versions.

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    35

    Re: Object reference not set to an instance of an object.

    ok...i kinda understand how that works. But I dont see how I can assign my arrays. Like, laptimes(i) = sr.readline, I only want it to ride the 3 lines with numbers in the txt file:

    Alain
    Ste-Julie
    03
    43
    40
    4/20/2006 12:33:55 AM

    Alain
    Ste-Julie
    02
    32
    40
    4/20/2006 12:34:00 AM

    VB Code:
    1. Private Sub btnplusminus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnplusminus.Click
    2.         Dim i, num As Integer
    3.         Dim sr As IO.StreamReader = IO.File.OpenText(path)
    4.         Dim laptimes As String() = System.Text.RegularExpressions.Regex.Split(sr.ReadToEnd(), Environment.NewLine)
    5.  
    6.         Do While (sr.Peek <> -1)
    7.             sr.ReadLine()
    8.             num += 1
    9.         Loop
    10.         sr.Close()
    11.         Dim srr As New IO.StreamReader(path)
    12.         For i = 1 To num
    13.             laptimes(i) = (srr.ReadLine)
    14.         Next
    15.         srr.Close()
    16.     End Sub

    Version 2003

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,448

    Re: Object reference not set to an instance of an object.

    Calling ReadToEnd reads the entire file and returns an single String. Once you've done that Peek will return -1 so your loops are useless. A StreamReader can only move forward. Once it reaches the end of the file that's it. If you want to treat different lines in your file in different ways then you're going to need to do more work. Think about waht you would do if you were reading it with your eyes and using a pen and paper. You would read the file in blocks of five lines and you'd treat each of those five lines in a different way. That's what you'll need to do in your code. You can either process the lines as you read them or else read the entire file and then process the lines from your array.

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    35

    Re: Object reference not set to an instance of an object.

    But I dont get why my old code (the first one I posted) doesnt work. Doesnt it make sense? Everything works fine until it gets to the laptime(i) part. I've seen that
    VB Code:
    1. For i = 1 To num
    2. laptimes(i) = sr.ReaLine
    3. lstreport.Items.Add(laptimes(i))
    4. Next

    work before. So why doesnt it work NOW?!

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,448

    Re: Object reference not set to an instance of an object.

    This line:
    VB Code:
    1. Dim laptimes As String() = System.Text.RegularExpressions.Regex.Split(sr.ReadToEnd(), Environment.NewLine)
    reads the entire file into a single string, splits that string on the line breaks and returns an array where each element is a line from the file. Why do you now want to go back and read the file again? There is no point because you already have its entire contents. Appart from that, the code you're using won't read it anyway. The contents of your Do loop will not get executed because Peek will return -1 immediately for the very reason Iposted previously: ReadToEnd reads the entire file and so the StreamReader will be at the end of the file afterwards. Because your Do loop isn't executed your 'num' variable is zero so your For loop will begave very differently to what you expect. It will read the first line of the file into the second element of the array, then read the second line into the first element of the array and then exit. Like I said, raeding the file again is COMPLETELY pointless because you already have the entire contents. Also, arrays are zero-based, which means that their indexes go from 0 to (Length - 1), not 1 to Length. You should read the section about arrays in the Start VB.NET tutorial in my signature because your understanding of how they behave in VB.NET is lacking.

    You should also familiarise yourself with with the capabilities of the debugger. Yopu should have set a breakpoint at the top of that code and stepped through it line by line. You could then follow the values of any variable, property or expression at each line so you can see exactly what your code is doing.

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