0

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 title-screen 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.TitleScreen1ドル.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.LwjglApplication1ドル.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

These are my codes:

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();
 }
}
Reaz Masud
24.3k15 gold badges83 silver badges102 bronze badges
asked Mar 19, 2020 at 22:06

1 Answer 1

1

From the crash log, it looks like the problem is in your TitleScreen where you are adding a click listener. I am referring to the following line.

game.setScreen(new GameScreen(game));

Looks like the game object is not initialized before and hence you are getting a NullPointerException.

answered Mar 19, 2020 at 23:59
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!! After adding the line this.game = aGame; it worked!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.