Search code examples
javalibgdx

NullPointerException on stage.act()


When I run my game and click the screen to exit my splash screen it should bring me to my MainMenu but instead it just freezes. I found the line of code that the error's were telling me but it was no help.(MainMenu.java:124) that line just showed stage.act(); than stage.draw(); if I tried to remove or comment out stage.act();.(MyGdxGame.java:31) then pointed at my super.render();. I have no idea what's wrong, any help at all would be great.

Errors:

Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.jack.mygdxgame.screens.MainMenu.render(MainMenu.java:124)
    at com.badlogic.gdx.Game.render(Game.java:46)
    at com.jack.mygdxgame.MyGdxGame.render(MyGdxGame.java:31)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)

MyGdxGame Code:

package com.jack.mygdxgame;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.jack.mygdxgame.screens.GameScreen;
import com.jack.mygdxgame.splash.SplashScreen;

public class MyGdxGame extends Game {

    public static int nWidth;
    public static int nHeight;
    private Game game; 

    public MyGdxGame() {
        game = this;
    }

    @Override
    public void create() {
        nWidth = Gdx.graphics.getWidth();
        nHeight = Gdx.graphics.getHeight();

        setScreen(new SplashScreen(this));
    }

    @Override
    public void render() {
        super.render();
    }
}

MainMenu code:

package com.jack.mygdxgame.screens;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.jack.mygdxgame.MyGdxGame;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import static com.badlogic.gdx.graphics.Pixmap.Format.RGB888;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;

public class MainMenu implements Screen {

    Stage stage;
    Skin skin;
    private Game game;

    public MainMenu(Game game) {
        this.game = game;
    }

    public void create() {
        // load assets

        int buttonOffSet = 20;

        stage = new Stage();
        Gdx.input.setInputProcessor(stage);// Make the stage consume events

        createBasicSkin();
        TextButton newGameButton = new TextButton("Start game", (com.badlogic.gdx.scenes.scene2d.ui.Skin) skin); // Use the initialized skin
        newGameButton.setPosition(Gdx.graphics.getWidth() / 2 - Gdx.graphics.getWidth() / 8, Gdx.graphics.getHeight() / 2 + (newGameButton.getHeight() + buttonOffSet));
        newGameButton.addListener(new ClickListener() {

            @Override
            public void clicked(InputEvent event, float x, float y) {
                System.out.println("Start game button clicked");
            }
        });

        stage.addActor(newGameButton);

        TextButton loadButton = new TextButton("Load game", (com.badlogic.gdx.scenes.scene2d.ui.Skin) skin); // Use the initialized skin
        loadButton.setPosition(Gdx.graphics.getWidth() / 2 - Gdx.graphics.getWidth() / 8, Gdx.graphics.getHeight() / 2);
        loadButton.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                System.out.println("Load Button clicked");
            }
        });

        stage.addActor(loadButton);

        TextButton settingsButton = new TextButton("Settings", (com.badlogic.gdx.scenes.scene2d.ui.Skin) skin);
        settingsButton.setPosition(Gdx.graphics.getWidth() / 2 - Gdx.graphics.getWidth() / 8, Gdx.graphics.getHeight() / 2 - (settingsButton.getHeight() + buttonOffSet));
        settingsButton.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                System.out.println("Settinga Button clicked");
            }

        });

        stage.addActor(settingsButton);

        TextButton exitGameButton = new TextButton("Exit game", (com.badlogic.gdx.scenes.scene2d.ui.Skin) skin);
        exitGameButton.setPosition(Gdx.graphics.getWidth() / 2 - Gdx.graphics.getWidth() / 8, Gdx.graphics.getHeight() / 2 - 2 * (newGameButton.getHeight() + buttonOffSet));
        exitGameButton.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                System.out.println("Exit game button clicked");
                System.exit(0);
            }
        });

        stage.addActor(exitGameButton);
    }

    private void createBasicSkin() {
//Create a font
        BitmapFont font = new BitmapFont();
        skin = new Skin();
        skin.add("default", font);

        //Create a texture
        Pixmap pixmap = new Pixmap((int) Gdx.graphics.getWidth() / 4, (int) Gdx.graphics.getHeight() / 10, Pixmap.Format.RGB888);
        pixmap.setColor(Color.WHITE);
        pixmap.fill();
        skin.add("background", new Texture(pixmap));

        //Create a button style
        TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
        textButtonStyle.up = skin.newDrawable("background", Color.GRAY);
        textButtonStyle.down = skin.newDrawable("background", Color.DARK_GRAY);
        textButtonStyle.checked = skin.newDrawable("background", Color.DARK_GRAY);
        textButtonStyle.over = skin.newDrawable("background", Color.LIGHT_GRAY);
        textButtonStyle.font = skin.getFont("default");
        skin.add("default", textButtonStyle);

    }

    @Override
    public void show() {
    }

    @Override
    public void render(float f) {
        GL20 gl = Gdx.graphics.getGL20();
        gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        Gdx.gl.glClearColor(1, 1, 1, 1);

        stage.act();
        stage.draw();
    }

    @Override
    public void resize(int i, int i1) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void hide() {
    }

    @Override
    public void dispose() {
    }

}

Solution

  • As I assume you never called create method of MainMenu screen. so stage is not initialized and throw NullPointer exception.

    use show method for initialization of your screen variable.