javaandroidnullpointerexceptionbuzztouch

My app crashes when I run it from Android Studio after making a change while it's already running


I'm working on an application in Android Studio that, for some reason, constantly throws the error "Unfortunately, MyApp has stopped". This only happens if I run the app, make a change to the code while it's running, and then try to run it again. If I stop the "Run" task and start it again, I don't get this error.

Here's the log:

06-14 15:54:06.213 25752-25752/com.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp, PID: 25752
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.activity_host}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.myapp.MB_item.getItemId()' on a null object reference
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077)
  at android.app.ActivityThread.-wrap15(ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:148)
  at android.app.ActivityThread.main(ActivityThread.java:5417)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.myapp.MB_item.getItemId()' on a null object reference
  at com.myapp.BT_screen_menuButtons$override.onCreateView(MB_screen_menuButtons.java:119)
  at com.myapp.BT_screen_menuButtons$override.access$dispatch(MB_screen_menuButtons.java)
  at com.myapp.BT_screen_menuButtons.onCreateView(MB_screen_menuButtons.java:0)
  at android.app.Fragment.performCreateView(Fragment.java:2220)
  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1130)
  at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1953)
  at android.app.FragmentController.dispatchActivityCreated(FragmentController.java:152)
  at android.app.Activity.performCreateCommon(Activity.java:6232)
  at android.app.Activity.performCreate(Activity.java:6239)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077) 
  at android.app.ActivityThread.-wrap15(ActivityThread.java) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350) 
  at android.os.Handler.dispatchMessage(Handler.java:102) 
  at android.os.Looper.loop(Looper.java:148) 
  at android.app.ActivityThread.main(ActivityThread.java:5417) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Why would the code behave differently only on subsequent launches and not when I initially run the app?

Additional code:

This is the line (119 of MB_screen_menuButtons.java) that's throwing the error. screenData.getItemId() is clearly where the null value is being passed but I don't understand why it would only be null during an Instant Run update.

debugger.showIt(fragmentName + ":onCreateView JSON itemId: \"" + screenData.getItemId() + "\" itemType: \"" + screenData.getItemType() + "\" itemNickname: \"" + screenData.getItemNickname() + "\"");

Solution

  • This appears to be an issue with Instant Run being incompatible with something in your code - potentially a library that generates code.

    You can disable Instant Run and it should fix this problem:

    Preferences > Build, Execution, Deployment > Instant Run > Uncheck "Enable Instant Run"

    If you want to keep Instant Run you can just UnCheck "Restart Activity on Code Changes"

    Preferences > Build, Execution, Deployment > Instant Run > Uncheck "Restart Activity on Code Changes"