This has been bugging me for a few hours now. On each new ListViewItem
I add, I associate a Name value to it, so it doesn't add the same item twice. The value is a URL, I'm not sure if perhaps that's the issue?
The error I am getting is:
The object reference is not set to an instance of an object.
Full error description:
System.NullReferenceException: The object reference is not set to an instance of an object. By System.Windows.Forms.ListView.ListViewItemCollection.FindInternal (String key, Boolean searchAllSubItems, ListViewItemCollection listViewItems, ArrayList foundItems) At System.Windows.Forms.ListView.ListViewItemCollection.Find (String key, Boolean searchAllSubItems)
Here's my code:
I get the error at If Not ListView2.Items.Find(lv.Name, False).Count >= 1 Then
Dim m_url As String = HttpUtility.HtmlDecode(matchUrl)
m_url = m_url.Replace(" ", "")
If Not m_url.Contains(rootdomain) Then
Dim lv As New ListViewItem
lv.Name = m_url
If Not ListView2.Items.Find(lv.Name, False).Count >= 1 Then
lv.Text = m_url
lv.SubItems.Add(StripTags(match.Groups(2).Value))
lv.SubItems.Add("")
ListView2.Items.Add(lv)
End If
e_links_c += 1
End If
I tried to not use the Name at all, but then I got a few errors which stated something like the key was already added, I have no idea why that happended since I didn't associate any Name value to the items.
Updated code:
Dim lv As New ListViewItem(m_url)
lv.Name = m_url
If ListView2.Items.IndexOfKey(lv.Name) = -1 Then
lv.SubItems.Add(StripTags(match.Groups(2).Value))
lv.SubItems.Add("")
ListView2.Items.Add(lv)
End If