Search code examples
javaandroidsocket.ioonrestoreinstancestate

Android null pointer exception when trying to set player turn using onRestoreInstanceState


I am doing a simple multiplayer socket.io tic tac toe game. So far everything is working, however, when I try to rotate the screen I am getting a null pointer exception error. I have two override methods to save and restore the values of my player turn's boolean value.

When I try to directly set the value like this player1.setMyTurn(savedInstanceState.getBoolean("playerTurn")) It gives me a null pointer exception.

Does anyone know why?

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putBoolean("playerTurn", player1.isMyTurn());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    player1.setMyTurn(savedInstanceState.getBoolean("playerTurn")); //this seems to cause the issue
}

Error:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.twochicken.multiplayertictactoe, PID: 8612
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.twochicken.multiplayertictactoe/com.twochicken.multiplayertictactoe.MainActivity}: java.lang.NullPointerException
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738)
                  at android.app.ActivityThread.access$900(ActivityThread.java:135)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:136)
                  at android.app.ActivityThread.main(ActivityThread.java:5017)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:515)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                  at dalvik.system.NativeStart.main(Native Method)
               Caused by: java.lang.NullPointerException
                  at com.twochicken.multiplayertictactoe.MainActivity.onRestoreInstanceState(MainActivity.java:416)
                  at android.app.Activity.performRestoreInstanceState(Activity.java:916)
                  at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1138)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738) 
                  at android.app.ActivityThread.access$900(ActivityThread.java:135) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:136) 
                  at android.app.ActivityThread.main(ActivityThread.java:5017) 
                  at java.lang.reflect.Method.invokeNative(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:515) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                  at dalvik.system.NativeStart.main(Native Method) 

Solution

  • Try to invert the lines in the first method

    @Override
    protected void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putBoolean("playerTurn", player1.isMyTurn());
        super.onSaveInstanceState(savedInstanceState);
    }
    

    And in second make sure that you have the data before use it.

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
         if (savedInstanceState != null) {       
              player1.setMyTurn(savedInstanceState.getBoolean("playerTurn")); 
         }
    }
    

    A tip I give to you is to create a constant instead of "playerTurn", this can make your code more easly maintainable