Sign in to follow this  
glBender

c# nullObjectReference exception

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by SamLowry
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.

*** Source Snippet Removed ***


rating +++ for you sir [wink]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this