0

I've been having trouble adding my own custom font my game, and was wondering why libgdx doesn't like drawing my new stage i've set up once my class is called?

The class is called whenever an item intersects with another and the main class calls this class's .render() method. I want it to draw the string "game over" to the screen, however i am getting an error at stage.act(delta):

Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.mygdx.game.GameOverScreen.render(GameOverScreen.java:70)
    at com.mygdx.game.MyGdxGame.render(MyGdxGame.java:191)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:214)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)

Here is my code:

class GameOverScreen implements Screen {
    Label gameoverstring;

    private Stage stage;
    SpriteBatch spritebatch;

    BitmapFont white;
    LabelStyle headingStyle;

    @Override
    public void show() {
        stage = new Stage();
        white = new BitmapFont(Gdx.files.internal("new.fnt"), false);
        headingStyle = new LabelStyle(white, Color.WHITE);

        gameoverstring = new Label("game ovaaaa!", headingStyle);
        gameoverstring.setFontScale(3);
        stage.addActor(gameoverstring);
    }

    @Override
    public void render(float delta) {
        //gameoverstring.setPosition(0, 0);
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act(delta);
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void hide() {
    }

    @Override
    public void dispose() {
    }
}
P.T.
  • 24,557
  • 7
  • 64
  • 95

1 Answers1

0

Something on line 70 (in your render method) is null. See What is a NullPointerException, and how do I fix it? for some background.

I suspect your problem is that the stage in render is null. Probably because your show method hasn't been invoked yet.

Community
  • 1
  • 1
P.T.
  • 24,557
  • 7
  • 64
  • 95