Sign in to follow this  
JefferyThomasSventora

[XNA] Font Class Issue

Recommended Posts

Hello, I just started programming in C++ and C# about 4-5 months ago so i am still fairly new to the syntax although I do know quite a few things about it. Just recently I indulged myself within XNA (I am going to school to become a Game Developer). I started off with displaying 2D Sprites and then I moved into displaying text. In the "Game1.cs" I was able to load and display text but then I tried to create my own "GameFont" class.

[code] class CGameFont
{
private SpriteFont GFONT;
private Vector2 GFONT_POS;
private String GFONT_TEXT;
private SpriteBatch FONT_SB;
private Color FONT_COLOR;

public CGameFont(float X, float Y, SpriteFont FONT, Color F_COLOR, SpriteBatch SB)
{
GFONT_POS = new Vector2(X, Y);
GFONT = FONT;
FONT_SB = SB;
FONT_COLOR = F_COLOR;
}

public void DISPLAY()
{
GFONT_TEXT = GFONT_TEXT + "\n";
Vector2 GFONT_ORIGIN = new Vector2(0.0f, 0.0f);
FONT_SB.DrawString(GFONT, GFONT_TEXT, GFONT_POS, FONT_COLOR, 0, GFONT_ORIGIN, 1.0f, SpriteEffects.None, 0.0f);
}

public void V2_SETPOS(Vector2 NEWPOS)
{
GFONT_POS = NEWPOS;
}

public void ADDTEXT(String TxtToAdd)
{
Vector2 NEWPOS = new Vector2(GFONT_POS.X, GFONT_POS.Y - 14);
V2_SETPOS(NEWPOS);

if (GFONT_TEXT != null)
GFONT_TEXT = GFONT_TEXT + "\n" + TxtToAdd;
else
GFONT_TEXT = TxtToAdd;

DISPLAY();
}
}[/code]

Now, maybe I'm missing something, but this problem has stumped me all day long... whenever I run this code while trying to Add Text to the object,. it stops and highlights:

[code]FONT_SB.DrawString(GFONT, GFONT_TEXT, GFONT_POS, FONT_COLOR, 0, GFONT_ORIGIN, 1.0f, SpriteEffects.None, 0.0f);[/code]

And points to:

[b]GFONT_ORIGIN[/b] "NullReferenceException was unhandled" (object reference not set to an instance of an object.)

Which makes no sense to me because when I put my mouse over GFONT_ORIGIN it is clearly a vector2...

Thanks in advance to any help.

[i]-Jeff Sventora[/i]

Share this post


Link to post
Share on other sites
Flimflam    665
Vector2 is a value type, so it can't be a null reference and as such isn't what's giving you the error.

More than likely, FONT_SB or GFONT is the object that's null. Check to make sure either of those aren't null.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this