So I’m trying to draw a grid to the screen with monogame using an array, but it keeps giving me the error “object reference must be set to instance of object”. I’ve initialized the grid as you can see, so I don’t know whats causing the problem. The exception is being thrown on the Globals.Spritebatch.draw line, and when I change the grid[i,j] to new Rectangle(), it works just fine, but I would prefer to use the grid. Here’s my code, any help/explanation of what I’m doing wrong would be appreciated.
public class Grid
{
Rectangle[,] grid;
public Grid()
{
Rectangle[,] grid = new Rectangle[12, 8];
for (int i = 0; i <= 11; i++)
{
for (int j = 0; j <= 7; j++)
{
grid[i, j] = new Rectangle(new Point(100 + 50 * i, 100 + 50 * j), new Point(50, 50));
}
}
}
public void Update()
{
}
public void Draw()
{
for(int i = 0; i <= 11; i++)
{
for (int j = 0; j <= 7; j++)
{
Globals.spriteBatch.Draw(Globals.content.Load<Texture2D>("2d\\GridRect"), grid[i, j], Color.White);
}
}
}
}