[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

System.NullReferenceException was unhandled

Posted on 2013-01-12
9
Medium Priority
?
499 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

656 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