开发者

Unexplainable Null Reference Exception in VB.NET

开发者 https://www.devze.com 2023-02-15 05:14 出处:网络
I have an application for working with files. It needs to work with the files one character at a time. I am using an ArrayL开发者_运维知识库ist to store the data. Here\'s the code that\'s causing the

I have an application for working with files. It needs to work with the files one character at a time. I am using an ArrayL开发者_运维知识库ist 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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号