0
Dim item = ListView2.Items.Add("...")
    For cnt As Integer = 1 To ListView2.Columns.Count
        item.SubItems.Add("")
    Next
    If ListView2.FocusedItem.ImageIndex = 10 Then
        If TextBox2.Text.Length < 4 Then
            TextBox2.Text = ""
        End If
    End If

The error I got is here:

If ListView2.FocusedItem.ImageIndex = 10 Then

Error name: object reference not set to an instance of an object

====================================================================

This is how it's really looks like:

Private Sub ListView2_DoubleClick(sender As Object, e As EventArgs) Handles ListView2.DoubleClick
    If ListView2.FocusedItem.ImageIndex = 0 Or ListView2.FocusedItem.ImageIndex = 1 Or ListView2.FocusedItem.ImageIndex = 2 Then
        If TextBox2.Text.Length = 0 Then
            TextBox2.Text += ListView2.FocusedItem.Text
        Else
            TextBox2.Text += ListView2.FocusedItem.Text & "\"
        End If
        RefreshList()
    End If
    'from here doesn't work
    If Not IsNothing(ListView2.FocusedItem) AndAlso ListView2.FocusedItem.ImageIndex = 10 Then
        If TextBox2.Text.Length < 4 Then
            TextBox2.Text = ""                
        Else
            TextBox2.Text = TextBox2.Text.Substring(0, TextBox2.Text.LastIndexOf("\"))
            TextBox2.Text = TextBox2.Text.Substring(0, TextBox2.Text.LastIndexOf("\") + 1)
            RefreshList()
        End If
    End If
End Sub

Public Sub RefreshList()
    ListView2.Items.Clear()
    Dim item = ListView2.Items.Add("...")
    For cnt As Integer = 1 To ListView2.Columns.Count
        item.SubItems.Add("")
    Next     
End Sub
  • basically the Listview2 is already fulled on Form_Load and i just want to add a new item with the text "..." so the List2iew fills up itself with directories and files and i want to stay the new first item name "..." which means, in this case, 'back'. So it's shows now the item name whenever the refresh method is called, but it's do nothing so it's false..How can i fix that? – user3131716 Dec 24 '13 at 09:56

1 Answers1

0

Try to check for ListView2.FocusedItem to make sure it isn't Null/Nothing, before accessing its property (ImageIndex property in this case). Following is an example of how to add the checking:

If Not IsNothing(ListView2.FocusedItem) AndAlso ListView2.FocusedItem.ImageIndex = 10 Then

The condition after AndAlso will only be checked if condition before it is True.

har07
  • 88,338
  • 12
  • 84
  • 137
  • But if it's false how can i access that property without giving me that error? – user3131716 Dec 24 '13 at 09:04
  • If it is false, then you don't need to access the property. Why bother checking for a property of a non existent -yet- object. Read http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it for reference about the error you got. – har07 Dec 24 '13 at 09:44