MrAnthology
MrAnthology

Reputation: 23

Null Reference Exception in XNA

I have a problem with XNA. I'm trying to get text to "breathe" by expanding and contracting, but it won't run correctly. It compiles fine, but I keep getting a "NullReferenceException: Object reference not set to an instance of an object." error once it runs. I've got two files, Game1.cs and BreathingText.cs.

BreathingText.cs:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TestingThings
{
    class BreathingText
    {       
        public void drawText(string textToDraw, int X, int Y)
        {
            int fontRestraint = 1;
            Game1 game = new Game1();
            GraphicsDeviceManager graphics = game.graphics;
            SpriteBatch sB = game.spriteBatch;
            SpriteFont font = game.gameFont;
            Vector2 FontPos = new Vector2(X, Y);
            Vector2 StrCenter = new Vector2(0,0); //font.MeasureString(textToDraw) / 2;

             sB.Begin();
             sB.DrawString(font,
                           textToDraw,
                           FontPos,
                           Color.LightGreen,
                           0,
                           StrCenter,
                           1.0f,
                           SpriteEffects.None,
                           0.5f);
            if (fontRestraint == 1)
            {
                font.Spacing += 0.25F;
            }
            else if (fontRestraint == 0)
            {
                font.Spacing -= 0.25F;
            }
                if (font.Spacing == 17)
                {
                    fontRestraint = 0;
                }
                else if (font.Spacing == 0)
                {
                    fontRestraint = 1;
                }
            sB.End();
        }
    }
}

The exception happens at sB.Begin(). I thought it was happening because spriteBatch in Game1.cs wasn't initializing, but it looks to me like it should. Here's Game1.cs:

namespace TestingThings
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public GraphicsDeviceManager graphics;
        public SpriteBatch spriteBatch;
        public SpriteFont gameFont;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            gameFont = Content.Load<SpriteFont>("Celestia");
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            { this.Exit(); }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            BreathingText breath = new BreathingText();
            breath.drawText("test", graphics.GraphicsDevice.Viewport.Width / 2, graphics.GraphicsDevice.Viewport.Height - 25);
            base.Draw(gameTime);
        }
    }
}

I'm not sure why it's not working, because it seems like it should. However, I am a newbie and the solution is most likely right under my nose. I had the BreathingText.cs code in the Draw method in Game1 and it worked fine, until I moved it to it's own class.

Any help would be appreciated.

Thanks, MrAnthology

Upvotes: 2

Views: 2457

Answers (1)

George Duckett
George Duckett

Reputation: 32418

You are creating a new instance of your game1 class in the Drawtext method (Game1 game = new Game1();), so that game's spritebatch is never assigned a value (it'll be null).

You should pass in the instance of your game object when you create your text class, then use that within your drawtext method.

class BreathingText
{
    private Game1 _Game1; // Use this were you have 'game' currently

    public BreathingText(Game1 game)
    {
        _Game1 = game;
    }

Upvotes: 2

Related Questions