Solved

System.NullReferenceException was unhandled

Posted on 2013-01-12
9
488 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 74

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 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 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
 

Author Comment

by:jampost
ID: 38771073
Kaufmed,

  Are you saying list the file names in the code?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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 85

Accepted Solution

by:
Mike Tomlinson earned 250 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now