# c# nullObjectReference exception

## Recommended Posts

glBender    156
This is the code that is giving me the exception...
    try
{
m_rtiItiles = new RTile[80, 20];

for (int h = 0; h < 20; h++)
{
for (int w = 0; w < 80; w++)
{
//This throws the exception
Console.Write(m_rtiItiles[w, h].GetSymbol());
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}

I dont understand it... At first i thought the tile array wasnt being initialized somehow, so i added the allocation code to this function, still didnt work. I thought maybe GetSymbol() was the problem, but i've changed and now the only code in it is return 'a'. i'm sure this is simple and i just need some more caffeine. any help is appreciated

##### Share on other sites
SamLowry    1865
Assuming RTile is a class, then "new RTile[80,20]" is just making the array of pointers (or references, or how you want to call them), these are initialized to null.

You need to first create the array, then populate it.

tiles = new RTile[80,20];for ( int i = 0; i != tiles.GetLength(0); ++i ){  for ( int j = 0; j != tiles.GetLength(1); ++j )  {    tiles[i, j] = new RTile(/* constructor args */);  }}// tiles is ready for use

##### Share on other sites
glBender    156
yeah i just figured that out :( thanks for your time

##### Share on other sites
paulecoyote    1065
Quote:
 Original post by SamLowryAssuming RTile is a class, then "new RTile[80,20]" is just making the array of pointers (or references, or how you want to call them), these are initialized to null.You need to first create the array, then populate it.*** Source Snippet Removed ***

rating +++ for you sir [wink]