System.NullReferenceException was unhandled

I have an error that I cannot resolve. “System.NullReferenceException was unhandled”


What I am trying to do is select images from a combo box so they can be displayed in a picture box. The variable "dra" is the trouble maker for sure.


The Code:

Public Class Orders

    Private m_path As String = "C:\Users\Joe\Documents\Visual Studio 2012\Projects\sa_Shipping-Receiving\My Project\Images\VL"

    Private Sub Orders_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadPictures()

    End Sub

 


   Private Sub LoadPictures()

        Dim dir As New IO.DirectoryInfo(m_path)
        Dim files As IO.FileInfo() = dir.GetFiles("*.jpg")
        Dim dra As IO.FileInfo

        'List the names of all files in the specified directory for each dra in files.



-----The line of code directly below is where the error occurred   (dra.Name). -----

        ComboBox1.Items.Add(dra.Name)




        On Error Resume Next
    End Sub



    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

        If Not (ComboBox1.SelectedItem Is Nothing) Then
            Me.PictureBox1.Image = Image.FromFile(IO.Path.Combine(m_path, ComboBox1.SelectedItem.ToString))
        End If

    End Sub

    End Class


Thank you for any help.
jampostGeneral MaanagerAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
You need to iterate over the files in your "files" array:
    Private Sub LoadPictures()
        Dim dir As New IO.DirectoryInfo(m_path)
        Dim files As IO.FileInfo() = dir.GetFiles("*.jpg")
        Dim dra As IO.FileInfo
        For Each dra In files
            ComboBox1.Items.Add(dra.Name)
        Next
    End Sub

Open in new window

0
 
käµfm³d 👽Commented:
Where are you initializing the variable dra at?
0
 
jampostGeneral MaanagerAuthor Commented:
Kaufmed,

I have been thinking that was the problem. I am not sure how to initial ize it.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
käµfm³d 👽Connect With a Mentor Commented:
I suppose that would depend on what lies within, "List the names of all files in the specified directory for each dra in files."
0
 
jampostGeneral MaanagerAuthor Commented:
Kaufmed,

  Are you saying list the file names in the code?
0
 
jampostGeneral MaanagerAuthor Commented:
The only reason I'm asking is because it is 207 files.
0
 
jampostGeneral MaanagerAuthor Commented:
They are all jpg files. I thought about getting it to pull up one of them to initialize it, but I couldn't get it to work.
0
 
jampostGeneral MaanagerAuthor Commented:
They are all in a single directory, and I can list them in the code if that is what needs to be done.
0
 
jampostGeneral MaanagerAuthor Commented:
kaufmed was giving me the answer, but I wasn't familiar with the terminology. Idle_Mind made it a bit clearer.  Thank you both.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.