Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NullReferenceException when adding item to generic list
#1
I have a little problem that I can't seem to solve.
When I try to add a value (integer) to a generic list (of integer), I always get a NullReferenceException.

This is my code:
[code2=vbnet]Dim listIndexes As List(Of Integer) = Nothing
For Each i As ListViewItem In Me.listKeys.Items
For Each i2 As ListViewItem In Me.listKeys.Items
If i.Text = i2.Text Then
listIndexes.Add(i.Index) 'The error persists here: NullReferenceException
listIndexes.Add(i2.Index) 'and propably an error here too
End If
Next
Next
Me.SelectKeys(listIndexes) 'This line just selects the items in the listview[/code2]

I've discovered that the index of these two items in the listview are both zero.

Hope someone can help,
brco
#2
Your problem lies within this line:

[code2=vbnet]Dim listIndexes As List(Of Integer) = Nothing[/code2]

So when you try to add items to the listIndexes variable, you are actually trying to add items to Nothing, hence your error. Thus, you need to initialize the list before putting anything into it. Try changing it to something like:

[code2=vbnet]Dim listIndexes As List(Of Integer) = New List(Of Integer)[/code2]
My Blog | My Setup | My Videos | Have a wonderful day.


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search ListBox + Convert ListBox.item to string brco900033 3 13,692 09-13-2012, 03:03 PM
Last Post: brandonio21
  Change full row select back colour or list view Bradley 3 20,295 05-23-2012, 06:54 PM
Last Post: brandonio21

Forum Jump:


Users browsing this thread: 1 Guest(s)