0

I'm currently working on a xna project, a 2D game. My problem is that when I want the bullet(poop) to appear after I pressed the Up key, i receive the error in the line which is giving the initial position to the Vector2 of my bullet(poop).

// for each bullet in the list, update it
foreach (Poop p in poopList)
{
   p.Update(gameTime);
}

In the Poop class, in the update method, i have a case, depending on the direction in which the bullet should go: Top (1) Right (2) Down (3) Left (4)

case 1:
                position = new Vector2(monitoPosition.X + monitoTexture.Width / 2 -     poopTexture.Width / 2, monitoPosition.Y + monitoTexture.Height / 2);

                position.Y = position.Y - speed;

                    if (position.Y <= 0 || position.Y >=500)
                        isVisible = false;
                    if (position.X <= 0 || position.X >= 800)
                        isVisible = false;

                break;

So in the line, in which I assign a new position to the bullet(poop), it throws the error.

Please Help

Ainvy
  • 3
  • 2

1 Answers1

0

A variable in your code is null, meaning you will get an Object reference not set an instance of an Object error, because it is basically nothing. See this StackOverflow question for more info.

Double check that all your variables are assigned to.

Assuming you are talking about this line:

position = new Vector2(monitoPosition.X + monitoTexture.Width / 2 -     poopTexture.Width / 2, monitoPosition.Y + monitoTexture.Height / 2);

Make sure that monitoPosition and poopTexture are set. You can set a breakpoint on this line to pause the code, and hover over the variables to see if they are null.

Cyral
  • 13,999
  • 6
  • 50
  • 90
  • I have now checked, and i find out that the poopTexture is the one that is null. How am I supposed to assign a texture to it; according to me, i already initialized it public void LoadContent(ContentManager Content) { monitoTexture = Content.Load("Monito"); foreach (Poop p in poopList) { p.LoadContent(Content); } } – Ainvy May 13 '14 at 01:11
  • Whats in your `p.LoadContent(` method? – Cyral May 13 '14 at 01:17
  • public void LoadContent(ContentManager Content) { poopTexture = Content.Load("Poop"); } – Ainvy May 13 '14 at 01:18
  • Actually, you should only load your textures ONCE, it is more efficient, and most importantly, with your current method, you need to load the content again every time you add a new `Poop` to `poopList`, because it's texture has not been assigned yet. – Cyral May 13 '14 at 01:18
  • To expand on that, make `poopTexture` a static variable, and when you call your game's `LoadContent`, do this `Poop.poopTexture = poopTexture = Content.Load("Poop");` – Cyral May 13 '14 at 01:19
  • Your welcome! Since your new to StackOverflow I might point out, that you can click the check mark next to any answer if it solves your question, it's a way to let others know you have found a solution and gives some reputation points to the answerer. – Cyral May 13 '14 at 14:37