2

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() + "\"");
Derek
  • 112
  • 1
  • 9
  • 5
    You have a `NullPointerException` at `MB_screen_menuButtons.java` at line 119. You should post the code of that function at least so we can help you – Sunshinator Jun 16 '16 at 17:29
  • Gimme a few minutes to post that code. Are you able to tell from what I posted so far why this would only happen on successive runs and not on an initial run? – Derek Jun 16 '16 at 17:35
  • @Kon : I know I have a NullPointerException. What I don't know is why it happens only on successive builds of the application. As I mentioned in the post, it doesn't happen when I run the app in the emulator. It only happens if I run it again without stopping the initial run. – Derek Jun 16 '16 at 17:38
  • 1
    Your question says "constantly", so that seems contradictory to "only first time" or "sometimes" – OneCricketeer Jun 16 '16 at 17:44
  • @cricket_007 : When I say "constantly", I mean that I can get it to happen every time that I try to re-run the app after changing code. It never happens the first time I run it, though. If I make a change and get the error, I can stop the process and immediately run it without making any changes and it works as intended. This *only* happens if I try to run the code with an instance of it already running. The emulator will close down the app and the process and I have to run again. – Derek Jun 16 '16 at 17:46
  • 2
    Sounds like something is faulty with the reload process of the emulator. In any case, please show the code. Your posts should include a [mcve] of the problem – OneCricketeer Jun 16 '16 at 17:47

1 Answers1

3

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"

Carlitos
  • 343
  • 5
  • 7
Adam S
  • 16,144
  • 6
  • 54
  • 81
  • This seems to be exactly where the issue is taking place. Being unfamiliar with Instant Run, it's hard for me to see what the difference is between an Instant Run and a regular run. I imagine that my only options, besides disabling Instant Run, are to continue tracing the code all the way to the source? Is there a way to set a breakpoint that only runs during Instant Run? I'm having a hard time wrapping my head around how to debug something that's getting swapped in by Instant Run. I could be overcomplicating it. – Derek Jun 16 '16 at 17:51
  • ive just had a similar issue, i was using 2.2 preview. The error continued after another day, restarting android studio seems to fix it temporally – PowerMan2015 Jun 16 '16 at 17:53
  • @PowerMan2015 - The issue *has* to be with my code, though, right? I can't imagine that this is an issue with Instant Run itself. – Derek Jun 16 '16 at 17:55
  • Another oddity - This doesn't happen if I add breakpoints and run. It only happens if I actually modify the code, even if it's just add '//' to an empty line. Instant Run must re-inject and re-run files but only if they've been edited without re-running previous methods. I'm marking this as the solution as it's definitely an issue with Instant Run that I'll have to investigate further. – Derek Jun 16 '16 at 18:00
  • I could quite out my finger on it . The results where erratic. It appeared to ignore amendments applied by instant run and run my old code. Then it stopped building all together . Have you confirmed if screen data values are null? – PowerMan2015 Jun 16 '16 at 18:01
  • Can you post the class / method . I'll take a look. Can't for out anything with just one line – PowerMan2015 Jun 16 '16 at 18:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114872/discussion-between-derek-and-powerman2015). – Derek Jun 16 '16 at 18:03
  • Based on personal experience, the issue is either with Instant Run itself (which is getting better with every release) or with a code-generating library such as Dagger, or Realm (which I've actually had a problem with when combined with Instant Run). – Adam S Jun 16 '16 at 18:10
  • this happened to me after i integrated java 8 in my project. – M.kazem Akhgary Nov 04 '18 at 22:02