I keep getting a "NullReferenceExeption was unhandled" with "Object reference not set to an instance of an object." written under it. I have all of the images (png) correct with names and added to references.

protected override void LoadContent()
    spriteBatch = new SpriteBatch(GraphicsDevice);
    backGround = Content.Load<Texture2D>("Cracked");
    player1.playerBlock = Content.Load<Texture2D>("square");
    player2.playerBlock = Content.Load<Texture2D>("square2");
  • \$\begingroup\$ Where do you declare spriteBatch or backGround? Do you have a stack trace? \$\endgroup\$ Oct 29, 2012 at 3:18
  • 2
    \$\begingroup\$ For one, if we're talking XNA here it's aTexture2DObject = Content.Load<Texture2D>("fileName"); Furthermore, are your objects player1 and player2 actually initialized by the time you reach this part of the code? \$\endgroup\$
    – Dialock
    Oct 29, 2012 at 3:31
  • \$\begingroup\$ The problem is with the "splayer1.playerBlock = Content.Load("square");" and "player1.playerBlock = Content.Load("square2");" lines \$\endgroup\$ Oct 29, 2012 at 3:34
  • 5
    \$\begingroup\$ Again, the player1 and player2 objects need to exist before you can modify their member variables. Your error message is telling you that the thing you want to change doesn't even exist... so create it. Like Player player1 = new Player(); \$\endgroup\$
    – Dialock
    Oct 29, 2012 at 4:24
  • \$\begingroup\$ I looked at my initialize class and it was like this: protected override void Initialize() { base.Initialize(); player1 = new Player(); player2 = new Player(); } \$\endgroup\$ Oct 29, 2012 at 21:59


Browse other questions tagged .