?
Solved

System.NullReferenceException was unhandled

Posted on 2013-01-12
9
Medium Priority
?
501 Views
Last Modified: 2013-01-12
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.
0
Comment
Question by:jampost
  • 6
  • 2
9 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38771032
Where are you initializing the variable dra at?
0
 

Author Comment

by:jampost
ID: 38771053
Kaufmed,

I have been thinking that was the problem. I am not sure how to initial ize it.
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1000 total points
ID: 38771057
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:jampost
ID: 38771073
Kaufmed,

  Are you saying list the file names in the code?
0
 

Author Comment

by:jampost
ID: 38771076
The only reason I'm asking is because it is 207 files.
0
 

Author Comment

by:jampost
ID: 38771083
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
 

Author Comment

by:jampost
ID: 38771089
They are all in a single directory, and I can list them in the code if that is what needs to be done.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 38771107
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
 

Author Closing Comment

by:jampost
ID: 38771290
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question