0

What is wrong with the below code, when i try to search different names or delete the written name in the textbox, i get the 'Object reference not set to an instance of an object' debug error? I have a textbox, a listbox and a webbrowser object:

Public Class BelgeAra
    Dim table As DataTable
    Private Sub BelgeAra_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Me.table = New DataTable
        With table.Columns
            .Add("Name", GetType(String))
            .Add("FullPath", GetType(String))
        End With
        'Load some data into the table
        Me.LoadRows("C:\My Files")
        'Bind the table to listbox1
        Me.ListBox1.DisplayMember = "Name"
        Me.ListBox1.ValueMember = "FullPath"
        Me.ListBox1.DataSource = Me.table



    End Sub
    Private Sub LoadRows(ByVal dir As String)
        If IO.Directory.Exists(dir) Then
            Try
                For Each fn As String In IO.Directory.GetFiles(dir, "*.pdf")
                    table.Rows.Add(IO.Path.GetFileNameWithoutExtension(fn), fn)
                Next
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        If Me.WebBrowser1.IsBusy Then
            Me.WebBrowser1.Stop()
        End If

        Dim pdf As String = Me.ListBox1.SelectedValue.ToString

        Me.WebBrowser1.Navigate(pdf)



    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        Dim i As Integer = ListBox1.FindString(TextBox1.Text)
        ListBox1.SelectedIndex = i

        If TextBox1.Text = "" Then
            ListBox1.SelectedIndex = -1
        End If


    End Sub
End Class
David Wilson
  • 4,369
  • 3
  • 18
  • 31
fritzz
  • 31
  • 2
  • 5
  • Plutonix, there is not enough answer about listbox on "What is a NullReferenceException, and how do I fix it?" topic, so this topic is not a duplicate. – fritzz Oct 26 '17 at 15:04
  • It doesn't matter if it contains an answer with a `ListBox` example or not. What makes your question a duplicate is that the answers in there explains what an NRE is, what could be causing one, and gives you tips and instructions to help you find out what's causing it. Thus it covers pretty much all questions having this problem. The top voted answers in there give you many examples on what you can do to track it down, if you read them. – Visual Vincent Oct 26 '17 at 15:51

0 Answers0