Sign in to follow this  

c# nullObjectReference exception

This topic is 4136 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

This topic is 4136 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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