My questions relates to this"Object reference not set to an instance of an object".

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Ive search around and I am trying to learn on my own. I know its good to ask around as well so here I go! After searching around, I am unable to find out the problems with my Windows Game coding in c#. When Debugging, the game beings. After scrolling
through the menu list and I click "Credits", I received this message. "Object reference not set to an instance of an object." Can you guess help me out? Thanks!<br/>
<br/>
**Credits Code**:<br/>
<br/>
<br/>
namespace DoomSchneider_The_Game<br/>
{<br/>
class Credits<br/>
{<br/>
GraphicsDeviceManager graphics;<br/>
SpriteBatch spriteBatch;<br/>
<br/>
Video creditsVid;<br/>
VideoPlayer creditsPlayer;<br/>
<br/>
Texture2D creditsTexture;<br/>
Rectangle creditsRectangle;<br/>
<br/>
<br/>
public Credits()<br/>
{<br/>
<br/>
<br/>
<br/>
}<br/>
<br/>
public void Initialize()<br/>
{<br/>
<br/>
creditsPlayer = new VideoPlayer();<br/>
<br/>
}<br/>
<br/>
public void LoadContent(ContentManager Content)<br/>
{<br/>
<br/>
creditsVid = Content.Load<Video>("Videos\Intro");<br/>
creditsRectangle = new Rectangle(0, 0, 100, 100);<br/>
<br/>
creditsPlayer.Play(creditsVid);<br/>
<br/>
}<br/>
<br/>
<br/>
public void Update(GameTime gameTime)<br/>
{<br/>
<br/>
<br/>
<br/>
}<br/>
<br/>
<br/>
public void Draw(SpriteBatch spriteBatch)<br/>
{<br/>
<br/>
creditsTexture = creditsPlayer.GetTexture();<br/>
<br/>
spriteBatch.Begin();<br/>
spriteBatch.Draw(creditsTexture, creditsRectangle, Color.White);<br/>
spriteBatch.End();<br/>
<br/>
<br/>
}<br/>
<br/>
}<br/>
}<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
**Menu Code:**<br/>
<br/>
<br/>
namespace DoomSchneider_The_Game<br/>
{<br/>
class MenuComponent<br/>
{<br/>
KeyboardState keyboard;<br/>
KeyboardState prevKeyboard;<br/>
<br/>
<br/>
MouseState mouse;<br/>
MouseState prevMouse;<br/>
<br/>
<br/>
//Song<br/>
Song menuSong;<br/>
<br/>
<br/>
SpriteFont spriteFont;<br/>
<br/>
<br/>
List<string> buttonList = new List<string>();<br/>
<br/>
<br/>
int selected = 0;<br/>
<br/>
<br/>
public MenuComponent()<br/>
{<br/>
buttonList.Add("Story");<br/>
buttonList.Add("Versus");<br/>
buttonList.Add("Online Play");<br/>
buttonList.Add("Extras");<br/>
buttonList.Add("Options");<br/>
buttonList.Add("Credits");<br/>
buttonList.Add("Exit");<br/>
<br/>
}<br/>
<br/>
public void LoadContent(ContentManager Content)<br/>
{<br/>
spriteFont = Content.Load<SpriteFont>("HUD");<br/>
<br/>
//Song<br/>
menuSong = Content.Load<Song>("Music\MainMenuMusic");<br/>
<br/>
MediaPlayer.Play(menuSong);<br/>
MediaPlayer.IsRepeating = true;<br/>
}<br/>
<br/>
public void Update(GameTime gameTime)<br/>
{<br/>
keyboard = Keyboard.GetState();<br/>
mouse = Mouse.GetState();<br/>
<br/>
if (CheckKeyboard(Keys.Up))<br/>
{<br/>
if (selected > 0) selected--;<br/>
}<br/>
if (CheckKeyboard(Keys.Down))<br/>
{<br/>
if (selected < buttonList.Count - 1) selected++;<br/>
}<br/>
if (CheckKeyboard(Keys.Enter) || CheckKeyboard(Keys.Space) || CheckKeyboard(Keys.E))<br/>
{<br/>
switch (selected)<br/>
{<br/>
case 0:<br/>
Game1.GameState = "Story";<br/>
break;<br/>
<br/>
case 1:<br/>
break;<br/>
<br/>
case 2:<br/>
break;<br/>
<br/>
case 3:<br/>
break;<br/>
<br/>
case 4:<br/>
break;<br/>
<br/>
case 5:<br/>
Game1.GameState = "Credits";<br/>
break;<br/>
<br/>
case 6:<br/>
Game1.GameState = "Exit";<br/>
break;<br/>
<br/>
}<br/>
}<br/>
<br/>
<br/>
prevMouse = mouse;<br/>
prevKeyboard = keyboard; <br/>
}<br/>
<br/>
public bool CheckMouse()<br/>
{<br/>
return (mouse.LeftButton ==ButtonState.Pressed && prevMouse.LeftButton == ButtonState.Released);<br/>
}<br/>
<br/>
public bool CheckKeyboard(Keys key)<br/>
{<br/>
return (keyboard.IsKeyDown(key) && !prevKeyboard.IsKeyDown(key));<br/>
}<br/>
<br/>
public void Draw(SpriteBatch spriteBatch)<br/>
{<br/>
Color color;<br/>
int linePadding = 3;<br/>
<br/>
spriteBatch.Begin();<br/>
for(int i = 0; i < buttonList.Count; i++)<br/>
{<br/>
color = (i == selected) ? Color.DarkGoldenrod : Color.Black;<br/>
spriteBatch.DrawString(spriteFont, buttonList, new Vector2((Game1.screen.Width/5) - (spriteFont.MeasureString(buttonList).X / 2),<br/>
(Game1.screen.Height/2) - (spriteFont.LineSpacing * buttonList.Count/2) + ((spriteFont.LineSpacing + linePadding) * i)), color);<br/>
<br/>
}<br/>
spriteBatch.End();<br/>
<br/>
}<br/>
<br/>
}<br/>
}<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
**Game Code(Default generated class, if needed)**<br/>
<br/>
<br/>
namespace DoomSchneider_The_Game<br/>
{<br/>
/// <summary><br/>
/// This is the main type for your game<br/>
/// </summary><br/>
public class Game1 : Microsoft.Xna.Framework.Game<br/>
{<br/>
GraphicsDeviceManager graphics;<br/>
SpriteBatch spriteBatch;<br/>
<br/>
//Menu Art<br/>
Texture2D background;<br/>
Rectangle backgroundRectangle;<br/>
<br/>
MenuComponent menucomponent;<br/>
StoryMenuComponent storymenucomponent;<br/>
Credits creditsClass;<br/>
<br/>
<br/>
public static string GameState = "Menu";<br/>
<br/>
public static Rectangle screen;<br/>
<br/>
public Game1()<br/>
{<br/>
graphics = new GraphicsDeviceManager(this);<br/>
Content.RootDirectory = "Content";<br/>
}<br/>
<br/>
/// <summary><br/>
/// Allows the game to perform any initialization it needs to before starting to run.<br/>
/// This is where it can query for any required services and load any non-graphic<br/>
/// related content. Calling base.Initialize will enumerate through any components<br/>
/// and initialize them as well.<br/>
/// </summary><br/>
protected override void Initialize()<br/>
{<br/>
screen = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);<br/>
<br/>
menucomponent = new MenuComponent();<br/>
storymenucomponent = new StoryMenuComponent();<br/>
<br/>
base.Initialize();<br/>
}<br/>
<br/>
/// <summary><br/>
/// LoadContent will be called once per game and is the place to load<br/>
/// all of your content.<br/>
/// </summary><br/>
protected override void LoadContent()<br/>
{<br/>
// Create a new SpriteBatch, which can be used to draw textures.<br/>
spriteBatch = new SpriteBatch(GraphicsDevice);<br/>
<br/>
//Menu Art<br/>
background = Content.Load<Texture2D>("BackGrounds\MainMenuArt2");<br/>
backgroundRectangle = new Rectangle(GraphicsDevice.Viewport.X, GraphicsDevice.Viewport.Y, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);<br/>
<br/>
menucomponent.LoadContent(Content);<br/>
storymenucomponent.LoadContent(Content);<br/>
<br/>
}<br/>
<br/>
/// <summary><br/>
/// UnloadContent will be called once per game and is the place to unload<br/>
/// all content.<br/>
/// </summary><br/>
protected override void UnloadContent()<br/>
{<br/>
// TODO: Unload any non ContentManager content here<br/>
}<br/>
<br/>
/// <summary><br/>
/// Allows the game to run logic such as updating the world,<br/>
/// checking for collisions, gathering input, and playing audio.<br/>
/// </summary><br/>
/// <param name="gameTime Provides a snapshot of timing values.</param><br/>
protected override void Update(GameTime gameTime)<br/>
{<br/>
// Allows the game to exit<br/>
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || GameState =="Exit")<br/>
this.Exit();<br/>
<br/>
switch (GameState)<br/>
{<br/>
case "Menu":<br/>
menucomponent.Update(gameTime);<br/>
break;<br/>
<br/>
case "Story":<br/>
storymenucomponent.Update(gameTime);<br/>
break;<br/>
<br/>
case "Credits":<br/>
creditsClass.Update(gameTime);<br/>
break;<br/>
}<br/>
<br/>
base.Update(gameTime);<br/>
}<br/>
<br/>
/// <summary><br/>
/// This is called when the game should draw itself.<br/>
/// </summary><br/>
/// <param name="gameTime Provides a snapshot of timing values.</param><br/>
protected override void Draw(GameTime gameTime)<br/>
{<br/>
GraphicsDevice.Clear(Color.White);<br/>
<br/>
spriteBatch.Begin();<br/>
spriteBatch.Draw(background, backgroundRectangle, Color.White);<br/>
spriteBatch.End();<br/>
<br/>
switch (GameState)<br/>
{<br/>
case "Menu":<br/>
menucomponent.Draw(spriteBatch);<br/>
break;<br/>
case "Story":<br/>
storymenucomponent.Draw(spriteBatch);<br/>
break;<br/>
}<br/>
base.Draw(gameTime);<br/>
}<br/>
}<br/>
}<br/><hr class="sig AsianAvery

View the full article
 
Back
Top