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