Skip to content
This repository has been archived by the owner on Feb 9, 2018. It is now read-only.

Crash: NullPointerException in NetworkService.java line 99 #25

Closed
vickychijwani opened this issue Apr 7, 2015 · 1 comment
Closed
Labels
Milestone

Comments

@vickychijwani
Copy link
Owner

java.lang.RuntimeException: Unable to start activity ComponentInfo{me.vickychijwani.spectre/me.vickychijwani.spectre.view.PostListActivity}: java.lang.RuntimeException: Could not dispatch event: class me.vickychijwani.spectre.event.LoadUserEvent to handler [EventHandler public void me.vickychijwani.spectre.network.NetworkService.onLoadUserEvent(me.vickychijwani.spectre.event.LoadUserEvent)]: Attempt to invoke interface method 'void me.vickychijwani.spectre.network.GhostApiService.getCurrentUser(retrofit.Callback)' on a null object reference
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
       at android.app.ActivityThread.access$800(ActivityThread.java:144)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5221)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.RuntimeException: Could not dispatch event: class me.vickychijwani.spectre.event.LoadUserEvent to handler [EventHandler public void me.vickychijwani.spectre.network.NetworkService.onLoadUserEvent(me.vickychijwani.spectre.event.LoadUserEvent)]: Attempt to invoke interface method 'void me.vickychijwani.spectre.network.GhostApiService.getCurrentUser(retrofit.Callback)' on a null object reference
       at com.squareup.otto.Bus.throwRuntimeException(Bus.java:458)
       at com.squareup.otto.Bus.dispatch(Bus.java:388)
       at com.squareup.otto.Bus.dispatchQueuedEvents(Bus.java:369)
       at com.squareup.otto.Bus.post(Bus.java:338)
       at me.vickychijwani.spectre.view.PostListActivity.onCreate(PostListActivity.java:82)
       at android.app.Activity.performCreate(Activity.java:5933)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
       at android.app.ActivityThread.access$800(ActivityThread.java:144)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5221)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void me.vickychijwani.spectre.network.GhostApiService.getCurrentUser(retrofit.Callback)' on a null object reference
       at me.vickychijwani.spectre.network.NetworkService.onLoadUserEvent(NetworkService.java:99)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.squareup.otto.EventHandler.handleEvent(EventHandler.java:89)
       at com.squareup.otto.Bus.dispatch(Bus.java:386)
       at com.squareup.otto.Bus.dispatchQueuedEvents(Bus.java:369)
       at com.squareup.otto.Bus.post(Bus.java:338)
       at me.vickychijwani.spectre.view.PostListActivity.onCreate(PostListActivity.java:82)
       at android.app.Activity.performCreate(Activity.java:5933)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
       at android.app.ActivityThread.access$800(ActivityThread.java:144)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5221)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Crashlytics issue

@vickychijwani vickychijwani added this to the 1.0 milestone Apr 7, 2015
@vickychijwani
Copy link
Owner Author

This occurs in the following scenario: when the app is in the background, the entire process may get killed, but activity back stack is maintained. So when the app is re-opened, Application#onCreate() is called again, which re-initializes the NetworkService, which sets mApi (and mAuthToken!) to null, which causes the NPE above.

See http://stackoverflow.com/a/14376919/504611 for more info. To reproduce, follow these steps: http://stackoverflow.com/a/18695974/504611, or: minimize the app and launch a few other apps, then lock the phone, unlock it after a minute and open the app again.

The obvious solution is to recreate mApi correctly on application startup, using the following algorithm:

  1. When user logs in, record the fact in a persistent boolean flag
  2. In Application#onCreate:
    • Construct and start the NetworkService
    • Now if we have a valid access token and the user was logged in, then recreate mApi using the access token
    • Else if we have the user's credentials and the user was logged in, then fire a LoginStartEvent
    • Else do nothing

@vickychijwani vickychijwani mentioned this issue Apr 7, 2015
3 tasks
vickychijwani added a commit that referenced this issue Apr 19, 2015
The problem is, cached data is never refreshed 😜
@vickychijwani vickychijwani changed the title NullPointerException in NetworkService.java line 99 Crash: NullPointerException in NetworkService.java line 99 May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant