C# XNA: What can cause SpriteBatch.End() to throw a NPE?
- by Rosarch
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....