Solved

System.NullReferenceException was unhandled

Posted on 2013-01-12
9
493 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 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared 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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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