Help with this error "NullReferenceExcepion was unhandled"

rusty009

Member
Joined
May 27, 2010
Messages
5
Programming Experience
1-3
Hey,

I am trying to make my program open a text file, store that text file into a string array, then have a function where you can input the line number you want and extract the data from that line of the text file,

this is my code

VB.NET:
Dim lines As String() = IO.File.ReadAllLines(TextBox37.Text)

Public Function ValuesExtract(ByVal line_number, ByVal lines)

        Dim val() As Integer = Nothing
        'declare variable to store individual line
        Dim values As String() = lines(line_number).Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)

        'extract first line of high capacitance values
        val(0) = values(2)
        val(1) = values(3)
        val(2) = values(4)
        val(3) = values(5)
        'increment line number to get next 4 capacitance values
        line_number = line_number + 1
        values = lines(line_number).Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)

        val(4) = values(1)
        val(5) = values(2)
        val(6) = values(3)
        val(7) = values(4)

        'increment again for the final 4 capacitance values
        line_number = line_number + 1
        values = lines(line_number).Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)

        val(8) = values(0)
        val(9) = values(1)
        val(10) = values(2)
        val(11) = values(3)


        Return val


when I run it it gives me an error saying "NullReferenceExcepion was unhandled" and that "object refernce is not set to an nstance of an object". Could some one please help me out ? Thanks.
 
Dim val() As Integer = Nothing
val(0) = values(2)
val(1) = values(3)
val(2) = values(4)
val(3) = values(5)

It might help if you had an array to work with. Or better yet, use a collection, like a List(Of Integer)
 
You declare Val() as an array and you set it to Nothing. Then you try to set 4 elements in the array that doesn't exist. That's like buying a car, working out a time to pick it up then driving it the same day (before you pick it up from the person). You have to have something before you can use it.
 
hmm, ok. Well the reason I set the array to nothing is because I was getting exactly the same error before, but I looked up the error and found a post that said set it to nothing when you declare it and you won't get the problem so that's what I tried. I still don't understand why I cant just,

dim val() as integer

and then just assign in values like,

val(1) = 1
val(2) = 2

why does this not work ?
 
Take a quick look at how arrays work:
VB.Net Array Basics
Arrays in VB .NET

I'm sure you'll see exactly what you'll need to do.

Also, this is why I like to use collections. You don't need to specify the total number of elements to allot for and you don't need to specify all of the elements at declaration either. You add as you go, you can remove from the beginning, the end or the middle without re-sizing the whole thing.
 
Back
Top