简体   繁体   English

VB.net数组引发异常

[英]VB.net Array throwing an exception

The following sub is throwing a 以下子句引发

"Object reference not set to an instance of an object." “你调用的对象是空的。”

exception. 例外。

For Each element As Song In modFiles.getSongs()
    Dim col(2) As String
    Dim item As ListViewItem
    col(0) = element.SongTitle
    col(1) = element.PlayTime
    col(2) = element.SongFilename
    item = New ListViewItem(col)
    setList.Items.Add(item)
Next

The exception is thrown on lines 抛出异常

col(0) = element.SongTitle
col(1) = element.PlayTime
col(2) = element.SongFilename

Any help would be appreciated 任何帮助,将不胜感激

Your array declaration is fine. 您的数组声明很好。

Your For Each iterator is returning a null object somewhere. 您的For Each迭代器在某处返回空对象。 Wrap a null test around the body of the loop. 在循环的主体周围包装一个空测试。

For Each element As Song In modFiles.getSongs()
    If element IsNot Nothing Then
        Dim col(2) As String
        Dim item As ListViewItem
        col(0) = element.SongTitle
        col(1) = element.PlayTime
        col(2) = element.SongFilename
        item = New ListViewItem(col)
        setList.Items.Add(item)
    End If
Next

您忘记了数组中的一个元素

Dim col(3) As String

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM