NullPointerException on BitmapFont

Hello guys,
I meet a strange problem. I want to use BitmapText to make a hud, so I create a class that call hud.java and setup the bitmapText. But when I run my project, it show the NullPointerException in line 61,
font = assetManager.loadFont("Interface/Fonts/Default.fnt");
And I call it by hud.addTeat();
So the problem is: Why show this error ? I already load a font for the BitmapText.
Thanks ! :grinning:

If this line is throwing a NullPointerException (you left out the stack trace so I can’t be sure) then it’s because assetManager is null. And if that’s true it’s because you never set it.

2 Likes

I alredy set the assetManager by:
public Hud (AssetManager assetManager) {}
and calling it by:
private AssetManager;
this.assetManager = app.getAssetManager();
Hud hud = new Hud(assetManager);
but I try to move the Hud code to a AppState, and it is working. I am not sure where the problem yet.

If the NPE is actually happening on that line. (We haven’t seen a stack trace hint hint hint hint hint hint hint.)
THEN 100% without a doubt double-super guarantee assetManager is null when that line is executed.

It is not anything else.

“But…”

No. It’s not anything else. 100% guaranteed.

Maybe you passed the AssetManager to your app state but didn’t set it? Or it’s set too late? Or? We can’t see the code that is the actual problem so we must just guess.

Here is the code that I create it:

        font = assetManager.loadFont("Interface/Fonts/Default.fnt");
        livesText = new BitmapText(guiFont,false);
        livesText.setLocalTranslation(10, screenHeight+530,0);
        livesText.setSize(40);
        livesText.setText("Lives: "+lives);
        guiNode.attachChild(livesText);

What is the stack trace?
What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?What is the stack trace?

3 Likes

also, show us the whole class… it’s the parts before this that are the problem.

Sorry, I already modifies the code by extends AbstractAppState, the BitmapFont is working, and I also missing stack trace. Realy sorry about that. :sweat_smile: :sweat_smile: :sweat_smile:

In the long run, you should extend BaseAppState and you will get a lot more useful helper methods.

Oh, I forgot the BaseAppState. I am more clearly about using AbstractAppState, but I will try the BaseAppState.Thanks for your remind. :smile:

BaseAppState provides better lifecycle methods for dealing with initialization versus enabled/disabling. It also has convenience methods for grabbing the Application and also other AppStates.

1 Like