-1

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
Bugs
  • 4,491
  • 9
  • 32
  • 41
Anders
  • 513
  • 2
  • 10
  • 32

1 Answers1

0

I'm not sure why the exception is thrown, but you can try the IndexOfKey() method instead. It will only iterate until it finds one match instead of many.

If ListView2.Items.IndexOfKey(lv.Name) = -1 Then '-1 = no item found.
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • It says -1 is an invalid argument. – Anders Apr 20 '17 at 19:23
  • @Anders : Could you give me a screenshot please? – Visual Vincent Apr 20 '17 at 19:25
  • This is weird man, it doesn't say it anymore. Please see the updated code I currently have above, but it still gives me: System.NullReferenceException: The object reference is not set to an instance of an object – Anders Apr 20 '17 at 19:33
  • It's also different from each run how many successful items it adds.. I don't get it.. – Anders Apr 20 '17 at 19:34
  • @Anders : Could you post a screenshot of the entire Visual Studio UI when the error is thrown then? Be sure to have the `Autos` window open (located in the bottom of the VS window, can be opened via the menu `Debug > Windows > Autos`). – Visual Vincent Apr 20 '17 at 19:37
  • @Anders : Please also post some more code so I can get a better understanding of what you're doing. – Visual Vincent Apr 20 '17 at 19:39
  • I'm currently trying out a few things, I appreciate you trying to help though, I'll get back to you when I have some more info. Thanks – Anders Apr 20 '17 at 19:46
  • Hello again, sorry for the inconvience, I suspect that the multiple threads I was running was conflicting with eachother, just tried to run 1 thread and did some changes, and it ran through them all without any problems. – Anders Apr 20 '17 at 19:53
  • 1
    @Anders : Background threads should not be accessing user interface elements. I hope you're not setting `Control.CheckForIllegalCrossThreadCalls` to `False` as well, because it should always be `True`. -- If you want to add, remove or edit items in the `ListView` you must do so on the UI thread. See my example on the documentation about how to invoke: http://stackoverflow.com/documentation/vb.net/1913/threading/6235/performing-thread-safe-calls-using-control-invoke#t=20170420201340726515 – Visual Vincent Apr 20 '17 at 20:13
  • Ya, I know man :/ I was running around corners, I'll check your doc and change it though. Thanks :) – Anders Apr 20 '17 at 20:24
  • @Anders : You can minimze the amount of code by using [**this extension method of mine**](http://stackoverflow.com/questions/41856986/multi-thread-haning-up-main-thread-going-in-order/41859866#41859866). If you use Visual Studio 2010 or higher you can then do for example: `Me.InvokeIfRequired(Sub() ListView2.Items.Add(lv))` (if you use VS2008 or lower you can still use my extension method, but not the `Sub()` lambda). – Visual Vincent Apr 20 '17 at 20:31