Issue with drawing to screen via an array

So I’m trying to draw a grid to the screen 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);
            }
        }
    }
}

Take a good look at the first line in your constructor. What is the scope of the variable you’re initializing?

I’m not sure what you mean. Could you explain?

You can use triple back ticks before and after your source code to make it look nicer. For inline source code just one back tick before and after.

` ` `

I put a space between the back ticks here to make the characters visible.

k cool, but how do i fix this?

The Debugger should tell you what reference is missing.

I’m betting it’s either the Globals.spriteBach being null or the Globals.content.Load() that is failing and you’re effectively passing in null to the Draw command. Let the debugger break where the error is and see what it’s complaining about. Take a look at the call stack and see where it actually blew up.

I would also avoid loading anything in your draw loop.

I figured it out. I initialized the array twice.

1 Like

Technically, you DEFINED it twice in two different scopes. You didn’t initialize the one in the class scope, but you did for the local one in the constructor as Trinith caught on the first reply.

Nice :slight_smile:

Also, for reference…

Why did I clean this code up?

public class Grid
{
	Rectangle[,] grid;
	
	public Grid()
	{
		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(“2d\GridRect”),
				grid[i, j],
				Color.White);
			}
		}
	}
}

Oh well, lol.

UPDATED

Should be…

	public Grid()
	{
		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));
			}
		}
	}
1 Like

Yeah I forgot to correct it :slight_smile:

Not his fault, having a programmers forum without a button to automatically format code is ridiculous.

EDIT: (nor a preview)

// What be thy speaking of?

But if you mean adding in code formatting for code that is not formatted correctly, you got to ask the question, should you be programming to begin with?

Joking aside, Perhaps you might know of a JS library which does this? and throw it I think @harry-cpp 's way?

I recently added code formatting for my blog but I always copied my code from my IDE, so, not tested if it formats but I doubt it. Tested and nope, has to be pre-formatted.

@Jjagg ping :stuck_out_tongue:

1 Like

Nevermind, I’m having a rough day and discourse is my favourite bullseye : )
Anyways, that’s blockquote, not code block. (blockquote is 1 quote, code block 3 quotes)
You could test the difference without polluting the forum if you had a preview : )

2 Likes

Oh you mean like this?

// hello
// world
// this is a comment

Still the same button… so, now, I am confused as to your meaning.

Anyway, this is solved.

And I solved my XBOX issue today, happy as a bee!

EDIT

missed the a lol