Jump to content
  • Advertisement
Sign in to follow this  
kubapl

[XNA] 'System.NullReferenceException'

This topic is 3617 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I Keep getting a runtime error of : An unhandled exception of type 'System.NullReferenceException' occurred in FontUtil.exe Additional information: Object reference not set to an instance of an object. I am trying to load a font resource inside a separate class that will handle my fonts. Here is my font class:
    class FontUtil
    {
        private SpriteFont m_gameFont;
        
        public Game1 Game1
        {
            get{ return game1; }
        }
        Game1 game1;

        public FontUtil()
        {
            m_gameFont = Game1.Content.Load< SpriteFont >( "Font/GameTestFont" );            
        }

        public void DrawFont( SpriteBatch spriteBatch )
        {
            spriteBatch.Begin( SpriteBlendMode.None );

            spriteBatch.DrawString( m_gameFont, "test", new Vector2( 10.0f, 10.0f ), Color.Black );

            spriteBatch.End();
        }
    }

Share this post


Link to post
Share on other sites
Advertisement
You've declared a private field of type Game1, but you never assign it a reference to an actual Game1 instance. Since you never do this, the field is null when you try to use it to get the ContentManager (hence, the NullReferenceException).

What you probably want to do is pass in a reference to your Game1 class in your FontUtil constructor, like this:


public FontUtil(Game1 game1)
{
this.game1 = game1;
m_gameFont = Game1.Content.Load< SpriteFont >( "Font/GameTestFont" );
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!