Question

I'm creating a game and for the moment I have 3 classes, greenpaddle, ball, and Game1.

When i run my game, the debugger hops down to my spriteBatch.Begin(); and says NullReferenceException Unhandled. This is my Game1.cs:

public class Game1 : Microsoft.Xna.Framework.Game
{

    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Ball ball;
    GreenPaddle gPaddle;
    Texture2D BackGround;


    public Game1()
    {

        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.PreferredBackBufferHeight = 500;
    }

    protected override void Initialize()
    {
        gPaddle = new GreenPaddle();
        ball = new Ball(gPaddle);
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        BackGround = Content.Load<Texture2D>("pongBG");
        gPaddle.LoadContent(Content);
        ball.LoadContent(Content);
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        gPaddle.Update(gameTime);//Error Line
        ball.Update(gameTime);

        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();//Error Line
        spriteBatch.Draw(BackGround, new Vector2(0f, 0f), Color.White);
        gPaddle.Draw(spriteBatch);
        ball.Draw(spriteBatch);
        spriteBatch.End();
        base.Draw(gameTime);
    }
}

Don't know what's wrong, this have never happened to me.

Was it helpful?

Solution

Because you initialized spritebatch...

spriteBatch = new SpriteBatch(GraphicsDevice);

...It shouldn't be null unless one of your other classes is changing it.

Things you can try:

-Put a breakpoint on it in load content, I don't know why it wouldn't be called, but just check in case, make sure that LoadContent() is being called.

-Rebuild you project and make sure your changes are being saved.


...As I was writing this answer and testing the code on my machine I finally found the error. I'll leave the advice above in case anyone else has one of those issues.

You are not calling base.Initialize in your Initialize() method. This method calls the internal XNA stuff, which results in your LoadContent() being called.

It is also a good idea to call base.LoadContent in the LoadContent() method, you should always call the base method on any overridden method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top