VB.NET 中无法解释的空引用异常
我有一个用于处理文件的应用程序。它需要一次处理一个字符的文件。我正在使用 ArrayList 来存储数据。以下是导致问题的代码:
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
抛出空引用异常的代码行是我(尝试)将 temp
的值分配给 fileData ArrayList< 中的新元素的地方。 /代码>。有人知道这里发生了什么事吗?谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,
fileData
设置为Nothing
,所以当您调用.Add
时,它当然会引发NullReferenceException
它。尝试将其设置为新实例:Well,
fileData
is set toNothing
, so of course it will fire aNullReferenceException
when you call.Add
on it. Try setting it to a new instance:您需要做的是将以下行更改为:
存在
有差异。我对字符串变量也经历过同样的事情,并且遇到了同样的问题。
尝试将值分配给“s”时会导致 NULL 指针。
没有。
What you need to do is change the following line:
To:
There is a difference. I have experienced the same thing with String variables and have gotten the same issue.
results in a NULL pointer when attempting to assign a value to 's'.
Does not.