Hi,
I'm trying to write a program that grabs data from a text file and
populates the array of a structure, but I've been getting a
"system.nullreference exception: object reference not set to an
instance of an object. at A.Readfile(string file, schedule{} &
flight)
at A.Main(String{} arg)
Sample code:
Public Structure Schedule
'members of the structure
Dim Airline As String
Public Sub Main(ByVal arg() As String)
Dim flights() As
Schedule 'a structure
previously defined
Dim response As String
ReadFile(arg(0), flights) 'i think flights is causing the problem, but not sure how
etc,
Sub Readfile(ByVal file As String, ByRef flight() As Schedule)
Dim sr As StreamReader
Dim fileName As String = Path.GetFullPath(file)
sr = New StreamReader(fileName)
etc., etc.
the program quits immediately when I execute it.
VB
underlines flights saying null reference can result at runtime due
to the variable being passed before it has been assigned a value. But
I've been able to do so with other (non-structure) arrays before,
(and with not such warning) and I can't see the difference with this
one. I'm not using objects, so it wouldn't make sense to add the New
(instance of) the debugger suggested for me.
Any help would be appreciated.

null reference in passing array between procedures
AWOLMAN
Hello! Try using a different method of reading in the file. Give this one a shot. It reads the data in line by line allowing you to throw it into for/next loops which can be handy when populating arrays or treeviews, etc:
FileOpen(1, "C:\MyFile.txt", OpenMode.Input)
Dim TEMPLINE As String
Do Until EOF(1)
TEMPLINE = LineInput(1)
'Throw in your code to deal with this new data
Loop
FileClose(1)
I hope this has shed some light on your issue.
RizwanSharp
albidochon
An array is an object - your attempting to create an array of schedule type which is obviously a user defined structure.
Ultimately you need to dimension the array to something before you can use it. This will allocate physical storage requirements and instantiate the array object.
Just like if you use a statement like dim x as Foo where foo is an object. Trying to use it before you create an instance using NEW will result in the same error. So in the objects case you should type dim x as NEW foo which will declare and instantiate an instance of the object.
In your case
Dim flights(10) as schedule - this will create and instantiate a 11 element array of type schedule.
Or you could use an arraylist which provide a little bit more flexibility as it is more like a collection but can be manipulated like a array, Array lists are great as you dont have to dimension them up front.
Dim x as new arraylist
x.add( "Item1")
x.add("Item2")
MMV2007
Redim flight(100)
or whatever other number is suitable.
You don't let ReadFile return the number of records that were read. To make the flight array exactly long enough, try code like this:
Dim count As Integer, alloc As Integer = 16
ReDim flight(alloc - 1)
Do Until sr.EndOfStream
If count >= alloc Then
alloc *= 2
ReDim Preserve flight(alloc - 1)
End If
With flight(count)
'--- Read data info flight(count)
...
End With
count = count + 1
Loop
sr.Close()
ReDim Preserve flight(count - 1)