question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

App crashes with NullPointerException on Android

See original GitHub issue

Describe the bug Here is some information: I’m using geofencing and location plugins in the app and app’s Application class looks like that:

...
class Application : FlutterApplication(), PluginRegistrantCallback {
 ...
    override fun registerWith(registry: PluginRegistry) {
        GeneratedPluginRegistrant.registerWith(registry)
    }
...
}
...

So, what’s the issue: when geofence event (user enters or exits specified area) is caught a method Application.registerWith(...) is called but Registrar doesn’t contain reference to the host activity for the location plugin. That’s why FlutterLocation.setActivity() is called with nullable activity and I see that this method is expecting that it might happen:

...
 void setActivity(@Nullable Activity activity) {
        this.activity = activity;
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(activity); 
...
    }
...

But LocationServices.getFusedLocationProviderClient(activity); expects non null activity and it throws a NullPointerException then application crashes. Could you please add try-catch block to handle this error ?

...
 void setActivity(@Nullable Activity activity) {
       try{
        this.activity = activity;
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(activity); 
...
       } catch(Exception e){
        e.printStackTrace();
       }
    }
...

Please this is an emergency!

Tested on:

  • Android, API Level 28 and higher

Other plugins:

  • geofencing
  • place_picker

Additional logs

E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to create service io.flutter.plugins.geofencing.GeofencingService: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Looper android.app.Activity.getMainLooper()' on a null object reference
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3965)
        at android.app.ActivityThread.access$1500(ActivityThread.java:219)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Looper android.app.Activity.getMainLooper()' on a null object reference
        at com.google.android.gms.common.api.GoogleApi.<init>(Unknown Source:50)
        at com.google.android.gms.location.FusedLocationProviderClient.<init>(Unknown Source:8)
        at com.google.android.gms.location.LocationServices.getFusedLocationProviderClient(Unknown Source:2)
        at com.lyokone.location.FlutterLocation.setActivity(FlutterLocation.java:87)
        at com.lyokone.location.LocationPlugin.registerWith(LocationPlugin.java:36)
        at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(GeneratedPluginRegistrant.java:40)
        at com.example.app.app.Application.registerWith(Application.kt:18)
        at io.flutter.plugins.geofencing.GeofencingService.startGeofencingService(GeofencingService.kt:74)
        at io.flutter.plugins.geofencing.GeofencingService.onCreate(GeofencingService.kt:114)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3953)
        at android.app.ActivityThread.access$1500(ActivityThread.java:219) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
zeienko-vitaliicommented, Mar 6, 2020

Hi, @Lyokone, unfortunately the problem isn’t solved, could you please look at new PR request #314

1reaction
Lyokonecommented, Mar 2, 2020

Great ! Was kinda obvious, but just wanted to be sure that you weren’t stuck 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android 11 app crashes when open with null pointer exception
Android 11 app crashes when open with null pointer exception ; performLaunchActivity (ActivityThread.java ; 3779) at android.app.ActivityThread.
Read more >
Android crash / NullPointerException · Issue #2477 - GitHub
I was able to reproduce it using my app (which is not terribly fancy) by setting minifyEnabled true, so it should be pretty...
Read more >
Bug: Application crashes due to NullpointerException while ...
1. Open Google Maps · 2. Start Navigation (For bike) · 3. Choose starting point · 4. Choose destination
Read more >
How to Fix the Top 5 Android Crashes - Instabug Blog
java.lang.NullPointerException ... Definitely the most common Android crash. Chances are if you've ever developed an Android app then you have run ...
Read more >
Crash on Android with NullPointerException - Mobile
I am using the blueprint functions for in-app purchases and so far the system w… ... Crash on Android with NullPointerException.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found