Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm using a 2D array of objects to store data about tiles, or "blocks" in my gameworld. I initialise the array, fill it with data and then attempt to invoke the draw method of each object.

foreach (Block block in blockList)
  {
    block.Draw(spriteBatch);
  }

I end up with an exception being thrown "Object reference is not set to an instance of an object".

What have I done wrong?

EDIT: This is the code used to define the array

Block[,] blockList;

Then

blockList = new Block[screenRectangle.Width, screenRectangle.Height];
  // Fill with dummy data
  for (int x = 0; x <= screenRectangle.Width / texture.Width; x++)
  {
    for (int y = 0; y <= screenRectangle.Height / texture.Width; y++)
    {
      if (y >= screenRectangle.Height / (texture.Width*2))
      {
        blockList[x, y] = new Block(1, new Rectangle(x * 16, y * 16, texture.Width, texture.Height), texture);
      }
      else
      {
        blockList[x, y] = new Block(0, new Rectangle(x * 16, y * 16, texture.Width, texture.Height), texture);
      }
    }
  }
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You are using a list. You've forgotten to prepopulate / structure it correctly.

Maybe you created your list so:

List<List<Block>> blockList = new ArrayList<List<Block>>();
//OR
ArrayList blockList = new ArrayList(); //older, non-generic ArrayList

Remember that a standard 1D list looks like this:

0|1|2|3

In your case you've created a list of lists, so you have this:

0[]|1[]|2[]|3[]

The square brackets represent the inner lists (of blocks) that sit within each outer list element. To clarify, when indexing your 2D list that would be blockList[outerIndex][innerIndex], which would return you a block, whereas just calling blockList[outerIndex] should return you a list of blocks. However, that is probably returning null, and the reason is as follows...

When you create the outer array (a list of lists), C# does not implicitly create the inner lists. You need to first declare and construct the outer list blockList as shown above, and then before you do any real work you need to initialise that by doing

for (int i = 0; i < desiredNumOfRowsPerColOrColsPerRow; i++)
{
    blockList.Add(new ArrayList<Block>());
}

If you had chosen to use a fixed size 2D array (Block[,] blockArray) you would not have experienced this problem.

share|improve this answer
    
Actually, I AM using a fixed size 2D array. I've updated the original question with the declaration code. –  Jacob Millward Nov 3 '12 at 12:49
    
Before the edit, you'd stated that this was a list. Have you tested the spritebatch for nullness using a trace? Because it's either the list or the spritebatch. Either way, once you find the null, work your way back from there. –  Nick Wiggill Nov 3 '12 at 12:57
    
@JacobMillwrad I notice you have "screenRectangle.Height / texture.Width". Is your texture the same width and height? –  Nick Wiggill Nov 3 '12 at 13:10
    
Yup, just a simple 16x16 texture. And I stated it was an array, however I've called it blockList. Confusing I know. –  Jacob Millward Nov 3 '12 at 13:39
1  
Nevermind, I fixed it myself by using: for (int x = 0; x <= screenRectangle.Width / texture.Width; x++) { for (int y = 0; y <= screenRectangle.Height / texture.Width; y++) { blockList[x, y].Draw(spriteBatch); } } –  Jacob Millward Nov 3 '12 at 13:42
show 1 more comment

I see three alternatives:

  • or blockList is null
  • or blocklist contains null values...
  • or blocklist contains values that are not of type block

I'd test with this code:

foreach (Block block in blockList)
{
  if (block == null)
  { 
      System.Diagnostics.Debugger.Break();
  }
  block.Draw(spriteBatch);
}
share|improve this answer
1  
¿-1? you should say why... –  Blau Nov 3 '12 at 12:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.