3

I am developing a game for Android using LibGDX. I have created several screens and I am able to visit them successfully if I have not already visited them it seems. For example, I can go from StartScreen to HomeScreen to LoginScreen to MainMenuScreen just fine. However, when I attempt to switch from my MainMenuScreen to the StartScreen of my application by clicking the logout button, I get a NPE:

Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.mathsvszombies2.game.Screens.MainMenuScreen$3.clicked(MainMenuScreen.java:145)
at com.badlogic.gdx.scenes.scene2d.utils.ClickListener.touchUp(ClickListener.java:89)
at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:58)
at com.badlogic.gdx.scenes.scene2d.Stage.touchUp(Stage.java:353)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:332)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:217)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)

I get the same error when I attempt to go from my SignUpScreen to LoginScreen after clicking the Sign Up button, however, I don't believe it is an error to do with the button because as I mentioned earlier, going to various screens initially works fine using button clicks (such as HomeScreen to LoginScreen using Login button). Thus, I believe there is a common error in my application. Let's focus on my MainMenuScreen to StartScreen error upon logout button click.

I start with declaring my Game variable at the top of the MainMenuScreen class:

private Game game;

This is my MainMenuScreen constructor:

public MainMenuScreen(Game game) {

    app.log("Screen", "Main Menu Screen");
    camera= new OrthographicCamera();
    viewport = new FitViewport(MathsVsZombies.V_WIDTH, MathsVsZombies.V_HEIGHT, camera);
    stage = new Stage(viewport);
}

This is where I attempt to switch screens:

logoutBtn.addListener(new ClickListener(){

        @Override
        public void clicked(InputEvent event, float x, float y) {
            LoginScreen.isLoggedIn = false;
            game.setScreen(new StartScreen(game));
        }
    });

Here is my StartScreen class - I start with declaring my Game variable at the top of my StartScreen class:

private Game game;

This is my StartScreen constructor:

public StartScreen(Game game){
    Gdx.app.log("Screen", "Start Screen");
    this.game = game;
    camera= new OrthographicCamera();
    viewport = new FitViewport(MathsVsZombies.V_WIDTH, MathsVsZombies.V_HEIGHT, camera);
    stage = new Stage(viewport);
    StartScreenActor actor = new StartScreenActor();
    stage.addActor(actor);
    Gdx.input.setInputProcessor(stage);
}

How can I fix this?

piet.t
  • 11,718
  • 21
  • 43
  • 52
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Tenfour04 Mar 07 '18 at 20:33

1 Answers1

0

Simply, all you have to do is add this line of code in your constructor method for any screen class you have:

this.game = game;

This is saying that this object is an instance of game. Without it, passing "game" into StartScreen when you say game.setScreen(new StartScreen(game)); has nothing to reference to, and therefore gives a NullPointerException.

updated MainMenuScreen constructor:

public MainMenuScreen(Game game) {
    this.game = game;
    app.log("Screen", "Main Menu Screen");
    camera= new OrthographicCamera();
    viewport = new FitViewport(MathsVsZombies.V_WIDTH, MathsVsZombies.V_HEIGHT, camera);
    stage = new Stage(viewport);
}

This is the only change you have to make, and changing screens will work just fine now.