What is object reference not set to an instance of an object?
-
Do While lcp_objReader.Peek() <> -1
str\_lcp\_Tmp = lcp\_objReader.ReadLine() str\_lcp\_data(a) = str\_lcp\_Tmp.Split(lcp\_delimiter, StringSplitOptions.RemoveEmptyEntries) If str\_lcp\_data(a).Length > 0 Then For index As Integer = 0 To str\_lcp\_data(a).Length - 1 Console.Write(str\_lcp\_data(a)(index) + "|") Next Console.Write(vbCrLf) a += 1 End If Loop
i would like to print out the row 120, so i put
Console.Write(str_lcp_data(120)(index) + "|")
After debug, it gave the message "object reference not set to an instance of an object". How come it become like this? If i want to print the rows after row 120 (from 121 until end of the file), should i initialise a to 120 before do while loop?
-
Do While lcp_objReader.Peek() <> -1
str\_lcp\_Tmp = lcp\_objReader.ReadLine() str\_lcp\_data(a) = str\_lcp\_Tmp.Split(lcp\_delimiter, StringSplitOptions.RemoveEmptyEntries) If str\_lcp\_data(a).Length > 0 Then For index As Integer = 0 To str\_lcp\_data(a).Length - 1 Console.Write(str\_lcp\_data(a)(index) + "|") Next Console.Write(vbCrLf) a += 1 End If Loop
i would like to print out the row 120, so i put
Console.Write(str_lcp_data(120)(index) + "|")
After debug, it gave the message "object reference not set to an instance of an object". How come it become like this? If i want to print the rows after row 120 (from 121 until end of the file), should i initialise a to 120 before do while loop?
this usualy accurs when you forget the initialize the object (the new keyword) for example:
Dim ll As List(Of String) ll.Add("test")
--> will give you're errorDim ll As new List(Of String) ll.Add("test")
--> will not give the error so in you're case I think you forgot to initialize the str_lcp_data object -
this usualy accurs when you forget the initialize the object (the new keyword) for example:
Dim ll As List(Of String) ll.Add("test")
--> will give you're errorDim ll As new List(Of String) ll.Add("test")
--> will not give the error so in you're case I think you forgot to initialize the str_lcp_data object -
that is not initialized (I think correct me if I'm wrong) that is declared to initialize the variable you need to enter data try this (just to see if this is the problem) str_lcp_data(120)="test" and then run and see if you still get the error if not you've found the cause wich is that there was no data to display if you still get the error then I don't really know what is going on
-
Do While lcp_objReader.Peek() <> -1
str\_lcp\_Tmp = lcp\_objReader.ReadLine() str\_lcp\_data(a) = str\_lcp\_Tmp.Split(lcp\_delimiter, StringSplitOptions.RemoveEmptyEntries) If str\_lcp\_data(a).Length > 0 Then For index As Integer = 0 To str\_lcp\_data(a).Length - 1 Console.Write(str\_lcp\_data(a)(index) + "|") Next Console.Write(vbCrLf) a += 1 End If Loop
i would like to print out the row 120, so i put
Console.Write(str_lcp_data(120)(index) + "|")
After debug, it gave the message "object reference not set to an instance of an object". How come it become like this? If i want to print the rows after row 120 (from 121 until end of the file), should i initialise a to 120 before do while loop?
You've created a jagged array (an array of arrays). When you do this each element points to another array. The array's initially aren't created. I think this is why you get the error. Take this for example
Dim data(5)() As String data(0) = New String() {"Data1", "Data2", "Data3"} Console.WriteLine(data(0)(0)) ' The next line will throw an error because the 2nd element ' doesn't point to an array yet. Console.WriteLine(data(1)(0))
What's probably happening is you are only reading x number of rows, so only the first x elements have been initialized properly. Trying to read beyond x will throw an error. You could initialize all the elements first like so:
For i As Integer = 0 To data.Length - 1 data(i) = New String() {} Next
Or you can just make sure you don't read beyond the number of lines read.