2012-02-24 85 views
0

以下子被投掷VB.net阵列抛出异常

“未设置为一个对象的实例对象引用”。

异常。

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 

抛出异常上线

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

任何帮助,将不胜感激

回答

1

您在数组中忘记一个元素

Dim col(3) As String 
+2

数组是0绑定,col(2)有三个项目。 – Jesse 2012-02-24 23:36:59

+0

同意。对我感到羞耻 – 2012-02-24 23:41:24

4

Your array declaration is fine.

您的For Each迭代器正在返回空对象。在循环的主体周围包装一个空测试。

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