0

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

2 Answers 2

4

Your array declaration is fine.

Your For Each iterator is returning a null object somewhere. 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
Sign up to request clarification or add additional context in comments.

Comments

1

You forgot one element in your array

Dim col(3) As String

1 Comment

Arrays are 0 bound, col(2) has three items.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.