VB.NET 中无法解释的空引用异常

发布于 2024-10-19 23:52:15 字数 393 浏览 2 评论 0原文

我有一个用于处理文件的应用程序。它需要一次处理一个字符的文件。我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转角预定愛 2024-10-26 23:52:15

好吧,fileData 设置为 Nothing,所以当您调用 .Add 时,它当然会引发 NullReferenceException它。尝试将其设置为新实例:

Dim fileData As New ArrayList

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
不顾 2024-10-26 23:52:15

您需要做的是将以下行更改为:

Dim temp As Char = Nothing  

存在

Dim temp as Char = ''

有差异。我对字符串变量也经历过同样的事情,并且遇到了同样的问题。

Dim s as String = nothing

尝试将值分配给“s”时会导致 NULL 指针。

Dim s as string = String.empty

没有。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文