Home All Groups Group Topic Archive Search About
Author
28 Mar 2005 12:56 PM
Glenn Grant via .NET 247
I am trying to create an Array of type Hash Table. When I try to assign a value to a key in the hash table i get a System.NullReferenceException error.

Here is the code:

Public Function ReadTilesFromFile(ByVal Path As String, Optional ByVal TilesetDir As String = "tilesets/")
        Dim i As Integer                                            ' Counter for loops
        Dim TilesetPath As String = TilesetDir & Path               ' Full path to tileset file
        Dim xDS As New Tileset()                                    ' Make xDS point to Tilset
        Dim xRow As Tileset.TilesRow                                ' Make xRow point to Tileset.TilesRow

        If New FileInfo(TilesetPath).Exists Then                    ' If the tileset file exists
            xDS.ReadXml(TilesetPath, _
                System.Data.XmlReadMode.IgnoreSchema)               ' Read tiles table from file

            If xDS.Tiles.Rows.Count > 0 Then                        ' Check if there are rows in the table
                For i = 0 To (xDS.Tiles.Rows.Count - 1)             ' For each row in the table
                    xRow = xDS.Tiles.Rows.Item(i)                   ' Use the current row of the table

                    ReDim Preserve aryTiles(i)

                    MsgBox("die")
                    MsgBox(aryTiles(i).Count)
                    ' AsciiTile
                    If Not xRow.IsAsciiTileNull() Then              ' If there is data in the AsciiTile field
                        'aryTiles(i).Add("AsciiTile", xRow.AsciiTile) ' Put the data in the Tiles hashtable
                        aryTiles(i).Item("AsciiTile") = xRow.AsciiTile
                    End If
                    MsgBox("die1")
                    ' Image
                    If Not xRow.IsImageNull() Then                  ' If there is data in the Image field
                        aryTiles(i).Add("Image", xRow.Image)        ' Put the data in the Tiles hashtable
                    End If
                Next i

            End If
        Else
            TilesetError(1)                                         ' Call TilesetError, MissingTilesetFile
        End If
    End Function

--------------------------------
From: Glenn Grant

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>Ks4h5KKv7EessyelBhdorg==</Id>

Author
28 Mar 2005 1:28 PM
Cor Ligthert
Glenn,

>I am trying to create an Array of type Hash Table. When I try to assign a
>value to a key in the hash table i get a System.NullReferenceException
>error.
>
Then there is probably an error in your program. Did it not tell where it
was or did you debug it so that you could see it?

Cor
Author
28 Mar 2005 2:10 PM
Chris Dunaway
Where in this code is the declaration for the array of hashtables?
Perhaps you posted the wrong section of code by mistake?
Author
28 Mar 2005 2:53 PM
Jay B. Harlow [MVP - Outlook]
Glenn,
>I am trying to create an Array of type Hash Table.
> When I try to assign a value to a key in the hash table
> i get a System.NullReferenceException error.

This normally occurs when you initialize an array without initializing the
elements. Hashtable is a reference type, when you use:

    Dim list(10-1) As Hashtable

or:

    Dim Preserve list(list.Length + 1)

The elements are initialized to Nothing, you need to initialize the
individual elements to a new Hashtable object, something like:

    Dim list(10-1) As Hashtable
    For index As Integer = 0 to list.Length - 1
        list(index) = New Hashtable
    Next

    ReDim Preserve list(list.Length)
    list(list.Length - 1) = New Hashtable

Hope this helps
Jay


Show quoteHide quote
"Glenn Grant via .NET 247" <anonym***@dotnet247.com> wrote in message
news:%23YdLWW5MFHA.2736@TK2MSFTNGP09.phx.gbl...
>I am trying to create an Array of type Hash Table. When I try to assign a
>value to a key in the hash table i get a System.NullReferenceException
>error.
>
> Here is the code:
>
> Public Function ReadTilesFromFile(ByVal Path As String, Optional ByVal
> TilesetDir As String = "tilesets/")
>        Dim i As Integer                                            '
> Counter for loops
>        Dim TilesetPath As String = TilesetDir & Path               ' Full
> path to tileset file
>        Dim xDS As New Tileset()                                    ' Make
> xDS point to Tilset
>        Dim xRow As Tileset.TilesRow                                ' Make
> xRow point to Tileset.TilesRow
>
>        If New FileInfo(TilesetPath).Exists Then                    ' If
> the tileset file exists
>            xDS.ReadXml(TilesetPath, _
>                System.Data.XmlReadMode.IgnoreSchema)               ' Read
> tiles table from file
>
>            If xDS.Tiles.Rows.Count > 0 Then                        ' Check
> if there are rows in the table
>                For i = 0 To (xDS.Tiles.Rows.Count - 1)             ' For
> each row in the table
>                    xRow = xDS.Tiles.Rows.Item(i)                   ' Use
> the current row of the table
>
>                    ReDim Preserve aryTiles(i)
>
>                    MsgBox("die")
>                    MsgBox(aryTiles(i).Count)
>                    ' AsciiTile
>                    If Not xRow.IsAsciiTileNull() Then              ' If
> there is data in the AsciiTile field
>                        'aryTiles(i).Add("AsciiTile", xRow.AsciiTile) ' Put
> the data in the Tiles hashtable
>                        aryTiles(i).Item("AsciiTile") = xRow.AsciiTile
>                    End If
>                    MsgBox("die1")
>                    ' Image
>                    If Not xRow.IsImageNull() Then                  ' If
> there is data in the Image field
>                        aryTiles(i).Add("Image", xRow.Image)        ' Put
> the data in the Tiles hashtable
>                    End If
>                Next i
>
>            End If
>        Else
>            TilesetError(1)                                         ' Call
> TilesetError, MissingTilesetFile
>        End If
>    End Function
>
> --------------------------------
> From: Glenn Grant
>
> -----------------------
> Posted by a user from .NET 247 (http://www.dotnet247.com/)
>
> <Id>Ks4h5KKv7EessyelBhdorg==</Id>
Author
28 Mar 2005 4:51 PM
Chris Dunaway
Jay B. Harlow [MVP - Outlook] wrote:
> The elements are initialized to Nothing, you need to initialize the
> individual elements to a new Hashtable object, something like:
>
>     ReDim Preserve list(list.Length)
>     list(list.Length - 1) = New Hashtable
>

Of course, if you are ReDim'ing the array by more than a single
element, you will need to use a loop too:

Dim oldlistlength As Integer = list.length
ReDim Preserve list(newlistlength - 1)
For x As Integer = oldlistlength - 1 To newlistlength - 1
   list(x) = New Hashtable
Next
Author
28 Mar 2005 6:53 PM
Chris Dunaway
I think in the For loop, it should be

For x As Integer = oldlistlength To newlistlength-1
   'Code
Next

my mistake