TypeInitializationException with 2D array

  • Thread starter Thread starter Fencer
  • Start date Start date
F

Fencer

Hello, if I want to create 2D-array of ints I can do:

int[,] ints = {{1, 2, 3}, {4, 5, 6}};

However, when I try the same thing for the type Tile, which is a class
I've written:
Tile[,] tiles = {
{new Tile(), new Tile(), new Tile(), new Tile()},
{new Tile(), new Tile(), new Tile(Tile.TileType.WALL), new Tile()},
{new Tile(Tile.TileType.START), new Tile(Tile.TileType.WALL),
new Tile(), new Tile(Tile.TileType.END)}};

I get a TypeInitializationException. I placed a breakpoint at the first
line in the two Tile constructors, but it's not reached before I get
the unhandled exception. How can I make it work?

- Fencer
 
Fencer said:
Hello, if I want to create 2D-array of ints I can do:

int[,] ints = {{1, 2, 3}, {4, 5, 6}};

However, when I try the same thing for the type Tile, which is a class
I've written:
Tile[,] tiles = {
{new Tile(), new Tile(), new Tile(), new Tile()},
{new Tile(), new Tile(), new Tile(Tile.TileType.WALL), new Tile()},
{new Tile(Tile.TileType.START), new Tile(Tile.TileType.WALL),
new Tile(), new Tile(Tile.TileType.END)}};

I get a TypeInitializationException. I placed a breakpoint at the first
line in the two Tile constructors, but it's not reached before I get
the unhandled exception. How can I make it work?

- Fencer

If the class Tile inherits from a base class, that base class
constructor will be called prior to the Tile constructors. Do you have
a base class and have you checked its constructor in the debugger?
 
Family said:
If the class Tile inherits from a base class, that base class
constructor will be called prior to the Tile constructors. Do you have
a base class and have you checked its constructor in the debugger?

Thanks, you got me looking in the right direction. The Tile class did
indeed inherit from another class but the problem turned out to be
something else which I noticed when I investigated this. I thought the
problem was due to my 2D-array, I should have tried allocating just a
single Tile object!

- Fencer
 
Back
Top