運行以下代碼時出現異常。VB.net對象數組拋出異常
Public Function getSongs() As Song()
' Dim dir As New DirectoryInfo(Application.ExecutablePath)
Dim dir As New DirectoryInfo(directory)
Dim songsInDir() As Song = Nothing
Dim i As Integer = 0
For Each file As FileInfo In dir.GetFiles()
'only read ".mp3" files
If file.Extension = ".mp3" Then
songsInDir(i) = New Song(file.Name)
i = +i
End If
Next
Return songsInDir
End Function
我得到行錯誤:
songsInDir(i) = New Song(file.Name)
我得到一個未捕獲的異常,說:「未將對象引用設置到對象的實例」
這首歌對象有:
Public Sub new(By Val filename as String)
...子,設置一個變量,並獲取文件信息(此代碼)
任何幫助,將不勝感激!
這是最好的解決方案。非常感謝! – 2012-02-24 23:39:22