Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > null reference exeption


Reply
 
Thread Tools Display Modes
  #1  
Old 07-13-2012, 11:06 PM
Amerigo's Avatar
Amerigo Amerigo is offline
Centurion
 
Join Date: Jan 2009
Location: USA
Posts: 127
Default null reference exeption


I get this unexplainable error when running this. Using a msgbox right before the error, everything has a value.

Code:
Public Class Form1
    Private ReadOnly RNG As New Random()
    Dim MyList As New List(Of String)
    Dim di As New IO.DirectoryInfo(Application.StartupPath & "\Images")
    Dim diar1 As IO.FileInfo() = di.GetFiles()
    Dim dra As IO.FileInfo
    Dim card() As String

    Private Sub Deal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Deal.Click
        Shuffle()
    End Sub

    Private Sub Shuffle()

        For Each dra In diar1
            MyList.Add(dra.FullName)
        Next

        While MyList.Count > 0
            For i As Integer = 1 To 4
                Dim index = RNG.Next(0, MyList.Count)
                card(i) = MyList(index) <<< ERROR HERE

                Select Case i
                    Case 1
                        Card1.Image = Image.FromFile(card(i))
                    Case 2
                        Card2.Image = Image.FromFile(card(i))
                    Case 3
                        Card3.Image = Image.FromFile(card(i))
                    Case 4
                        Card4.Image = Image.FromFile(card(i))
                    Case 5
                        Card5.Image = Image.FromFile(card(i))
                End Select
                MyList.RemoveAt(index)
            Next
        End While
    End Sub

End Class
__________________
Amerigo
Reply With Quote
  #2  
Old 07-14-2012, 07:54 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

Are you /sure/ everything has a value? Your code never initializes card().
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 07-14-2012, 08:07 AM
Amerigo's Avatar
Amerigo Amerigo is offline
Centurion
 
Join Date: Jan 2009
Location: USA
Posts: 127
Default

msgbox(card(i)) placed before the error pops up the randomly chosen file path as intended. What would I need to do to "initialize" Card()?

Never mind. I eliminated the (i) since I'm using 'Select Case i', I don't need it. Working now.
__________________
Amerigo
Reply With Quote
  #4  
Old 07-14-2012, 09:50 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

Code:
Dim card() As String
This line says "I want a variable named 'card' that can hold an array of Strings, but I don't want the array yet; I'll create it later." For an array to be initialized, you have to provide a size. Since you haven't, there's no sensible array to create so card holds Nothing.

You can initialize an array by giving it a size at creation:
Code:
Dim card(5) As String ' 6 elements
You can also set value with an initializer; VB will auto-calculate the size:
Code:
Dim card() As String = { "", "", "", "", "" }
After declaration, you have to use the ReDim statement to initialize the array:
Code:
ReDim(card, 5) ' 6 elements
That says "Create a 6-element array and assign it to the variable card."

If you don't do any of these steps, the variable is Nothing because it has not been assigned a value.

If that message box is working as you describe, there is code you aren't showing, and I can't tell you about what I can't see.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->