protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
kernel.spriteBatch.Begin();
if (overlay)
{
grey_out.Begin();
foreach (EffectPass pass in grey_out.CurrentTechnique.Passes)
{
pass.Begin();
kernel.current_state.draw();
pass.End();
}
grey_out.End();
draw_overlay();
}
else
{
kernel.current_state.draw();
}
kernel.spriteBatch.End();
base.Draw(gameTime);
}
Problem when calling SpriteBatch.End()
When I run my program I receive this error when it gets to the line kernel.spriteBatch.End();
NullReferenceException was unhandled:
Object reference not set to an instance of an object.
The only thing that this suggests to me is that I should check that kernel.spriteBatch isn't null but when I mouse over it, it appears to be a perfectly legitimate instance of SpriteBatch.
You sure error happens at that line? Check if kernel isn't null, and kernel.spritebatch isn't null
As far as I can tell that is the line and nothing is null, there is a member in spritebatch that's null but I'm not not sure if that's abnormal. Here's a picture
and the kernel is basically a structure that I want to hold my important stuff in
and the kernel is basically a structure that I want to hold my important stuff in
public struct Game_Kernel{ public SpriteBatch spriteBatch; public ContentManager content; public Game_State current_state; public GraphicsDeviceManager graphics;}
Here is all the code that starts everything. If nothing else I have to admit I've probably used some poor conventions, I start almost everything in the game's constructor and the Initialize and LoadContent methods are mostly empty.
public struct Game_Kernel { public SpriteBatch spriteBatch; public ContentManager content; public Game_State current_state; public GraphicsDeviceManager graphics; } public class Game1 : Microsoft.Xna.Framework.Game { Game_Kernel kernel; Effect grey_out; bool overlay; int overlay_index; delegate void Draw_Overlay(); Draw_Overlay draw_overlay; delegate void Update_Overlay(KeyboardState keyboard_state); Update_Overlay update_overlay; KeyboardState old_keyboard_state; Dictionary<string, Texture2D> overlay_elements; public Game1() { kernel = new Game_Kernel(); Content.RootDirectory = "Content"; kernel.graphics = new GraphicsDeviceManager(this); kernel.graphics.PreferredBackBufferWidth = 640; kernel.graphics.PreferredBackBufferHeight = 480; kernel.graphics.IsFullScreen = false; kernel.graphics.ApplyChanges(); Window.Title = "Game"; kernel.content = Content; kernel.spriteBatch = new SpriteBatch(GraphicsDevice); kernel.current_state = new Title(kernel); draw_overlay = new Draw_Overlay(draw_title); update_overlay = new Update_Overlay(update_title); old_keyboard_state = Keyboard.GetState(); overlay = true; overlay_index = 0; overlay_elements = new Dictionary<string,Texture2D>(); overlay_elements.Add("title_start", kernel.content.Load<Texture2D>("Textures/Buttons/title_start")); overlay_elements.Add("title_start_highlight", kernel.content.Load<Texture2D>("Textures/Buttons/title_start_highlight")); overlay_elements.Add("title_quit", kernel.content.Load<Texture2D>("Textures/Buttons/title_quit")); overlay_elements.Add("title_quit_highlight", kernel.content.Load<Texture2D>("Textures/Buttons/title_quit_highlight")); overlay_elements.Add("title_back", kernel.content.Load<Texture2D>("Textures/Buttons/title_back")); } protected void to_title() { draw_overlay = new Draw_Overlay(draw_title); update_overlay = new Update_Overlay(update_title); } protected override void Initialize() { grey_out = kernel.content.Load<Effect>("Grey_Out"); grey_out.CurrentTechnique = grey_out.Techniques["Grey_Out"]; base.Initialize(); } protected override void LoadContent() { } protected override void UnloadContent() { }
You should put all the loading in the LoadContent function as the graphics device will not be created / valid in the Game constructor. So put the spriteBatch creation and texture loading in LoadContent and see what happens
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement