Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7056297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:51:02+00:00 2026-05-28T03:51:02+00:00

I have a problem with XNA. I’m trying to get text to breathe by

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:51:03+00:00Added an answer on May 28, 2026 at 3:51 am

    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;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a problem with the stopping of a windows phone xna game. i
I have a problem with the logic of this code, XNA's Update Method is
I have problem with fancybox. I want to write a function that will run
i have problem to correctly bind data to WPF Chart. When i'm setting ItemsSource
I have just started trying to make a simple game with XNA 3.1 to
I'm currently trying to learn the ins and outs of XNA/C# but I'm currently
I have a problem in the game I wrote with XNA. I recently added
I'm having a problem with the XNA framework, I have a stripped down version
I've begun writing a game using XNA Framework and have hit some simple problem
I have a very subtle problem with XNA, specifically the SpriteBatch. In my game

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.