0

I have a working Listbox and everything seems to run smoothly except the fact that when I'm clicking on a not-item on the listbox, I'm getting an error:

"Object reference not set to an instance of an object."

Why do I get this message when I'm clicking on the white space in the listbox? I would understand better if I got the error when clicking on an actual item, but now I'm getting this error when I'm clicking on the non-item.

Here is where the error is shown on my MainForm:

Private Sub UpdateContactInformationFromRegistry()
    Dim contact As Contact = m_contacts.GetContact(listResults.SelectedIndex)

    cmbCountries.SelectedItem = DirectCast(contact.AddressData.Country, Integer)
    txtFirstName.Text = contact.FirstName
    txtLastName.Text = contact.LastName
    txtStreet.Text = contact.AddressData.Street
    txtZip.Text = contact.AddressData.ZipCode
    txtCity.Text = contact.AddressData.City
End Sub

m_contact.GetContact method:

Public Function GetContact(index As Integer) As Contact
    If index < 0 OrElse index >= m_contactRegistry.Count Then
        Return Nothing
    End If
    Return m_contactRegistry(index)

End Function

m_contactRegistry is a List

Public Class ContactManager
Private m_contactRegistry As List(Of Contact)

Public Sub New()
    m_contactRegistry = New List(Of Contact)()
End Sub

UPDATE V2 Listbox event

SelectedIndexChanged handler:

Private Sub listResults_SelectedIndexChanged(sender As Object, e As EventArgs) Handles listResults.SelectedIndexChanged
        UpdateContactInformationFromRegistry()
    End Sub
CodingSource
  • 203
  • 2
  • 15
John M
  • 3
  • 6
  • 1
    can you show the ListView related click code/event or otherwise indicate where it happens. if you call `UpdateContactInformationFromRegistry` from a selecteditem changed event, there will not be an item selected and all those things relying on it will die – Ňɏssa Pøngjǣrdenlarp Dec 30 '14 at 01:41
  • Which has *exactly the problem* @Plutonix mentioned: calling `UpdateContactInformationFromRegistry` from a selecteditem changed event. – Ken White Dec 30 '14 at 01:47
  • Bingo. Clicking on the nonItem area also fires SelectedIndexChanged. None of those things are checking to see if anything is selected. This is likely not to be your only encounter with the NRE: [Null Reference in Visual Basic](http://stackoverflow.com/a/26761773/1070452) – Ňɏssa Pøngjǣrdenlarp Dec 30 '14 at 01:48
  • So to fix this error I need to validate that an item is selected before runing **UpdateContactInformationFromRegistry**? – John M Dec 30 '14 at 01:51
  • you can prevent this error by testing value of selected item in UpdateContactInformationFromRegistry: if Not IsNull(listResults.Value) Then ... else exit sub end if – scraaappy Dec 30 '14 at 01:54

1 Answers1

0

See ListBox.SelectedIndex:

A value of negative one (-1) is returned if no item is selected.

So you could also do:

Private Sub listResults_SelectedIndexChanged(sender As Object, e As EventArgs) Handles listResults.SelectedIndexChanged
    If listResults.SelectedIndex <> -1 Then
        UpdateContactInformationFromRegistry()
    End If
End Sub
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40