2012-02-24 108 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