Array of Hash Tables

  • Thread starter Thread starter Glenn Grant via .NET 247
  • Start date Start date
G

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
 
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
 
Where in this code is the declaration for the array of hashtables?
Perhaps you posted the wrong section of code by mistake?
 
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
 
Jay said:
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
 
I think in the For loop, it should be

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

my mistake
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top