0

I have an application for working with files. It needs to work with the files one character at a time. I am using an ArrayList to store the data. Here's the code that's causing the problem:

Dim fileData As ArrayList = Nothing  
Dim temp As Char = Nothing  
While Not EOF(open_file_number)  
    Input(open_file_number, temp)  
    fileData.Add(temp)  
End While  

The line of code that is throwing the Null Reference Exception is where I (attempt to) assign the value of temp to a new element in the fileData ArrayList. Anybody have an idea of what's going on here? Thanks

Nate Koppenhaver
  • 1,676
  • 3
  • 21
  • 31

2 Answers2

1

Well, fileData is set to Nothing, so of course it will fire a NullReferenceException when you call .Add on it. Try setting it to a new instance:

Dim fileData As New ArrayList
mellamokb
  • 56,094
  • 12
  • 110
  • 136
  • OK I see it now `*smacks hand into forehead*` I thought the null reference was on temp – Nate Koppenhaver Mar 04 '11 at 04:26
  • @Nate: null references don't happen when passing arguments. It's perfectly valid to pass a `Nothing` for a parameter. null reference exception will only happen when attempting to access a property or call a method on a null object. – mellamokb Mar 04 '11 at 04:29
0

What you need to do is change the following line:

Dim temp As Char = Nothing  

To:

Dim temp as Char = ''

There is a difference. I have experienced the same thing with String variables and have gotten the same issue.

Dim s as String = nothing

results in a NULL pointer when attempting to assign a value to 's'.

Dim s as string = String.empty

Does not.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85