我在Class A
中定义了ArrayList
。然后我想在Class B
中构建这个数组,并在Class A
中使用它。在外部类中构建ArrayList - vb.net
我定义的ArrayList为:
Public arrayList As ArrayList
然后,在Class B
我做的:
Dim trLogkEmpty As New A
'Loop with strEspece definition
trLogkEmpty.arrayList.Add(strEspece)
'End Loop
程序抛出我这个错误:
NullReferenceException
我不知道为什么,因为strEspece
从来没有成为空(我测试过)。我不知道是否有其他原因。
另外,当我循环访问Class A
中的arrayList
元素时,我又得到NullReferenceException
。这是循环代码:
For Each logkNull In Me.arrayElemWithLogkEmpty
Console.WriteLine(logkNull)
Next
我不知道第一个异常会发生什么,但代码运行“正确”。在第二个异常中,我想这就像我失去了数组的元素值。我不知道如何解决它...任何帮助?我接受不同的方式来解决它!
您的类A的构造函数是否初始化'arrayList'? – Phylogenesis
在ClassA中,您需要创建ArrayList的一个实例。例如'Public arrayList As New ArrayList' – Blackwood
不,NRE更可能是因为'trLogkEmpty'或'arrayList'没有被初始化,而没有'strEspece' - 我们缺乏这个任何上下文,尽管没有真正的代码。请参阅[Visual Basic中的NullReference异常](http://stackoverflow.com/a/26761773/1070452)以获取有关此帮助的信息。 – Plutonix