# Foreach loop with 2d array of objects

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);
}
}
}
``````
-

## 2 Answers

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.

-
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 `null`ness 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. – Arcane Engineer Nov 3 '12 at 12:57
@JacobMillwrad I notice you have "screenRectangle.Height / texture.Width". Is your texture the same width and height? – Arcane Engineer 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
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

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);
}
``````
-
¿-1? you should say why... – Blau Nov 3 '12 at 12:39