XNA: What can cause SpriteBatch.End() to throw a NRE?

Posted by Rosarch on Stack Overflow See other posts from Stack Overflow or by Rosarch
Published on 2010-04-03T19:53:18Z Indexed on 2010/04/03 23:23 UTC
Read the original article Hit count: 230

Filed under:
|

I don't understand what I'm doing wrong here:

public void Draw(GameTime gameTime) // in ScreenManager
{
    SpriteBatch.Begin(SpriteBlendMode.AlphaBlend);
    for (int i = 0; i < Screens.Count; i++)
    {
        if (Screens[i].State == Screen.ScreenState.HIDDEN)
            continue;

        Screens[i].Draw(gameTime);
    }
    SpriteBatch.End(); // null ref exception
}

SpriteBatch itself is not null.

Some more context:

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

    public MasterEngine()
    {
        graphicsDeviceManager = new GraphicsDeviceManager(this);
        Components.Add(new GamerServicesComponent(this));

        // ...

        spriteBatch = new SpriteBatch(graphicsDeviceManager.GraphicsDevice);

        screenManager = new ScreenManager(assets, gameEngine, graphicsDeviceManager.GraphicsDevice, spriteBatch);
    }

//...

   protected override void Draw(GameTime gameTime)
    {
        screenManager.Draw(gameTime); // calls the problematic method
        base.Draw(gameTime);
    }
}

Am I failing to initialize something properly?

UPDATE:

As an experiment, I tried this to the constructor of MasterEngine:

spriteBatch = new SpriteBatch(graphicsDeviceManager.GraphicsDevice);

spriteBatch.Begin();
spriteBatch.DrawString(assets.GetAsset<SpriteFont>("calibri"), "ftw", new Vector2(), Color.White);
spriteBatch.End();

This does not cause a NRE. hmm....

UPDATE 2: This does cause an NRE:

protected override void Draw(GameTime gameTime)
{
    spriteBatch.Begin();
    spriteBatch.End(); // boned here
    //screenManager.Draw(gameTime);
    base.Draw(gameTime);
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about XNA