2D array error...

G

Guest

hi, im trying to create a 2D array of objects and ive done this before but i
must be forgetting something because i keep getting a null reference
exception at runtime....heres the code


Private arrTiles(7)() As Tile


Public Sub InitializeDisplay()

ReDim arrTiles(7)(7)

For i As Integer = 0 To 7

For j As Integer = 0 To 7

Dim tl As New Tile

tl.str = "red"
tl.Image = Image.FromFile("C:\Documents And
Settings\User\Desktop\Red.bmp")

''null reference error here!
arrTiles(i)(j) = tl

Next

Next

End Sub

any help would be great...
 
A

Armin Zingler

iwdu15 said:
hi, im trying to create a 2D array of objects and ive done this
before but i must be forgetting something because i keep getting a
null reference exception at runtime....heres the code


Private arrTiles(7)() As Tile

Public Sub InitializeDisplay()

ReDim arrTiles(7)(7)


You specified that item 7 points to an array containing 8 items. Item 0,
Item 1, Item 2 ... Item 6 are still Nothing. You would have to write:

for i as integer=0 to 7
redim arrtiles(i)(7)
next


For i As Integer = 0 To 7

For j As Integer = 0 To 7

Dim tl As New Tile

tl.str = "red"
tl.Image = Image.FromFile("C:\Documents And
Settings\User\Desktop\Red.bmp")

''null reference error here!
arrTiles(i)(j) = tl

Next

Next

End Sub

any help would be great...


You created an array of arrays, not a 2D array. If you wanted to create a
2D array, you would have to write:

Private arrTiles(7, 7) As Tile




Armin
 

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

Top