novoda/merlin

Please solve this NullPointerException

javierpe opened this issue · 13 comments

FATAL EXCEPTION: ConnectivityManager
                                                                     Process: com.taskpath.routes, PID: 13791
                                                                     java.lang.NullPointerException: Attempt to invoke virtual method 'void com.novoda.merlin.service.ConnectivityChangesForwarder.forward(com.novoda.merlin.receiver.ConnectivityChangeEvent)' on a null object reference
                                                                         at com.novoda.merlin.service.MerlinService$1.onConnectivityChanged(MerlinService.java:66)
                                                                         at com.novoda.merlin.receiver.ConnectivityCallbacks.notifyMerlinService(ConnectivityCallbacks.java:46)
                                                                         at com.novoda.merlin.receiver.ConnectivityCallbacks.onLost(ConnectivityCallbacks.java:34)
                                                                         at android.net.ConnectivityManager$CallbackHandler.handleMessage(ConnectivityManager.java:2425)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:168)
                                                                         at android.os.HandlerThread.run(HandlerThread.java:61)

Hi @FranciscoJavierPRamos we are sorry that you are experiencing a crash. Can you please update this ticket with the following please, so that we can have this problem resolved as quickly as possible 😄

  • Version of Merlin
  • Steps to reproduce
  • Can you provide the code you used to create the Merlin instance?
  • android version

I have yet to reproduce this issue but I can see how it might occur. It seems that

connectivityManager.unregisterNetworkCallback(connectivityCallbacks); is being unregistered on a HandlerThread as the above stacktrack shows. As this occurs on a handler thread we might receive a ConnectivityChange event before this has successfully unregistered. This event will be emitted through ConnectivityCallbacks which passes back to the service and the ConnectivityChangesForwarder which is now null throwing a NullPointerException.

We might need to unregister the ConnectivityCallbacks more explicitly and swallow if the listener is removed.

Please can you add the additional information requested above @FranciscoJavierPRamos when you are able. We will use this additional information to replicate and fix in one release.

I have discovered a way to reproduce this issue, it is a bit of a hack but works regardless and allows us to resolve the issue.

Steps:

  • Add breakpoints to ConnectivityCallbacks.notifyMerlinService.
  • Enter airplane mode in app
  • Breakpoint is hit, DO NOT CONTINUE DEBUGGER
  • Press back button in app to navigate to Merlin landing screen
  • CONTINUE DEBUGGER
  • Observe crash

THIS IS A HACK. Need reproducible steps and additional information.

Hi, thank you for you response. The steps are:

Merlin version: 1.1.4
Android version: 6.0.1

STEPS

1. Init Merlin
merlin = new Merlin.Builder() .withConnectableCallbacks() .withDisconnectableCallbacks() .withBindableCallbacks() .withAllCallbacks() .build(getActivity());

2. Register Bindable Callback

merlin.registerBindable(new Bindable() { @Override public void onBind(NetworkStatus networkStatus) { });

3. Register Connectable Callback

merlin.registerConnectable(new Connectable() { @Override public void onConnect() { });

4. Register Disconnectable Callback

merlin.registerDisconnectable(new Disconnectable() { @Override public void onDisconnect() { });

5. I closed the application and then this happens

FATAL EXCEPTION: ConnectivityManager Process: com.taskpath.routes, PID: 13791 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.novoda.merlin.service.ConnectivityChangesForwarder.forward(com.novoda.merlin.receiver.ConnectivityChangeEvent)' on a null object reference at com.novoda.merlin.service.MerlinService$1.onConnectivityChanged(MerlinService.java:66) at com.novoda.merlin.receiver.ConnectivityCallbacks.notifyMerlinService(ConnectivityCallbacks.java:46) at com.novoda.merlin.receiver.ConnectivityCallbacks.onLost(ConnectivityCallbacks.java:34) at android.net.ConnectivityManager$CallbackHandler.handleMessage(ConnectivityManager.java:2425) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:168) at android.os.HandlerThread.run(HandlerThread.java:61)

Thank you in advance!

Hi @FranciscoJavierPRamos, thanks for that additional information. I haven't been able to reproduce unfortunately. If you wouldn't mind, would you be able to pull this branch https://github.com/novoda/merlin/tree/MER-149/forwarder_null_pointer and publish to maven local to see if it resolves your issue?

Maven Local Release

Change the version in the build.gradle file, I've made it TEST here.
screen shot 2017-09-13 at 10 13 07

Build and run the following command, either from inside AndroidStudio or by running the associated command via the console.
screen shot 2017-09-13 at 10 12 30

In your app add mavenLocal to the list of repositories in the build.gradle file, replace the Merlin dependency version with the one you have changed it to in the first step (TEST).

Run the app and verify whether the issue has been resolved.

If you need any further help perhaps I can run you through it via email or something else.

@FranciscoJavierPRamos Have you had any luck reproducing with the above fix?

Hi, sorry for my late response, I have new issue, all happened when application is closing, please help me to reproduce this.

captura de pantalla 2017-09-28 a la s 12 04 47

@FranciscoJavierPRamos Which version did this occur with? Did this happen when you used the above local release? Does this happen on the same version of android mentioned above?

Hi @FranciscoJavierPRamos, can you try pulling our master branch and trying the above local release, I have just made some changes that should resolve this issue for you.

It's really important that you let me know if everything is ok so that I can make a release for all of the other people that rely on this library, thanks!

Hi again @FranciscoJavierPRamos, have you had any luck with this new build?

We are assuming that this issue has been resolved as of merlin v1.1.5. If any issues still persist, please open a new issue.

Hi ,I have occurred this issue again. My Merlin version is 1.1.5.
I add merlin for 2 activities to listen the network, is that correct what I did?

when I offline to go to one activity , then go to another activity, then back to the first activity, connect the network, it will be popped out this error seems the merlin has been killed but i have bind again, what can I do?
Michael

@Alubba can you open a new issue if you are experiencing an issue. I will try my best to help you but I need you to follow the template to speed up the process, thanks!

https://github.com/novoda/merlin/issues/new