【问题标题】:VB.net Array throwing an exceptionVB.net 数组抛出异常
【发布时间】:2012-02-24 23:23:59
【问题描述】:

下面的sub正在抛出一个

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

异常。

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

任何帮助将不胜感激

【问题讨论】:

    标签: arrays vb.net for-loop


    【解决方案1】:

    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
    

    【讨论】:

      【解决方案2】:

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

      Dim col(3) As String
      

      【讨论】:

      • Arrays are 0 bound, col(2) 有三个项。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多