Jump to content
  • Advertisement
Sign in to follow this  
glBender

c# nullObjectReference exception

This topic is 4351 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!