0
\$\begingroup\$

I'm trying to create an android game using libgdx with a Titlescreen, you just press start and your game startes. However I'm having difficulty trying to get these buttons to work. The titlescreen works, but as soon as I press 'start game' the game crashes with the error:

Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.dinab.onepiecev2.TitleScreen$1.clicked(TitleScreen.java:47)
at com.badlogic.gdx.scenes.scene2d.utils.ClickListener.touchUp(ClickListener.java:88)
at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:59)
at com.badlogic.gdx.scenes.scene2d.Stage.touchUp(Stage.java:351)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:360)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:221)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:128)

I don't understand what I'm doing wrong and whether the problem is in my TitleScreen.java or in my GameScreen.java

This is my code:

Main class

public class Onepiecev2 extends Game {
    static public Skin gameSkin;
    public void create () {
        gameSkin = new Skin(Gdx.files.internal("skin/glassy-ui.json"));
        this.setScreen(new TitleScreen(this));
    }

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

    public void dispose () {
    }
}

TitleScreen

public class TitleScreen implements Screen {

    Stage stage;
    Game game;
    SpriteBatch batch;
    Texture img;
    TextureRegion mainBackground;

    public TitleScreen(Game aGame){
        batch = new SpriteBatch();
        img = new Texture("start_screen.jpg");
        mainBackground = new TextureRegion(img, 0, 0, 1920, 1080);
        stage = new Stage(new ScreenViewport());
        Gdx.input.setInputProcessor(stage);
        Skin mySkin = new Skin(Gdx.files.internal("skin/glassy-ui.json"));

        Button start_btn = new TextButton("START GAME", mySkin);
        start_btn.setSize(Constantes.screenWidth/4, Constantes.screenHeight/12);
        start_btn.setPosition(Constantes.col_width*3,Constantes.row_height/3);
        start_btn.addListener( new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                game.setScreen( new GameScreen(game) );
            }
        } );

        stage.addActor(start_btn);

    }

    @Override
    public void show() {
        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(mainBackground, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        batch.end();
        stage.act();
        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() {
        stage.dispose();
        batch.dispose();
        img.dispose();
    }
}

GameScreen

public class GameScreen implements Screen {

    Stage stage;
    Game game;
    SpriteBatch batch;
    Texture img;
    TextureRegion mainBackground;

    public GameScreen(Game aGame){
        batch = new SpriteBatch();
        img = new Texture("start_screen.jpg");
        mainBackground = new TextureRegion(img, 0, 0, 1920, 1080);
        stage = new Stage(new ScreenViewport());
        Gdx.input.setInputProcessor(stage);
        Skin mySkin = new Skin(Gdx.files.internal("skin/glassy-ui.json"));

        Button start_btn = new TextButton("GO BACK", mySkin);
        start_btn.setSize(Constantes.screenWidth/4, Constantes.screenHeight/12);
        start_btn.setPosition(Constantes.col_width*3,Constantes.row_height/3);
        start_btn.addListener( new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                game.setScreen( new TitleScreen(game));
            }
        } );

        stage.addActor(start_btn);

    }

    @Override
    public void show() {
        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(mainBackground, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        batch.end();
        stage.act();
        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() {
        stage.dispose();
        batch.dispose();
        img.dispose();
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Are you setting game or is it null when the click handler is invoked? \$\endgroup\$
    – milk
    Commented Mar 20, 2020 at 3:00

1 Answer 1

1
\$\begingroup\$

It looks like you never assign the game member-variable in your TitleScreen class, so it is null when the click handler is invoked.

Try setting the game variable in the constructor of TitleScreen, you are already passing in a Game instance called aGame, you problably want to assign that to game:

public GameScreen(Game aGame){
    batch = new SpriteBatch();
    img = new Texture("start_screen.jpg");
    mainBackground = new TextureRegion(img, 0, 0, 1920, 1080);
    stage = new Stage(new ScreenViewport());
    game = aGame; // Add this 
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .