-2

Good day!

I am currently experiencing a dilemma where my code works on one of my Android devices but force closes on the other one.

Based on the my logcat, this is the error:

02-27 06:37:29.134 23579-23579/com.example.maui.dabtrain E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
                                                                              at com.example.maui.dabtrain.MainActivity.onMapReady(MainActivity.java:504)
                                                                              at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
                                                                              at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
                                                                              at android.os.Binder.transact(Binder.java:507)
                                                                              at com.google.android.gms.maps.internal.bw.a(:com.google.android.gms.DynamiteModulesB:82)
                                                                              at com.google.maps.api.android.lib6.impl.bf.run(:com.google.android.gms.DynamiteModulesB:1805)
                                                                              at android.os.Handler.handleCallback(Handler.java:751)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                              at android.os.Looper.loop(Looper.java:154)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6688)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

Here is the code for my location:

        gMap = googleMap;

        if(!gMap.isMyLocationEnabled())
            gMap.setMyLocationEnabled(true);

        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);


        if (myLocation == null) {
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            String provider = lm.getBestProvider(criteria, true);
            myLocation = lm.getLastKnownLocation(provider);
        }


    LatLng userLocation = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
    gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 13), 1500, null);

The rest of the code is for Google Markers, Polyline, Radius, etc.

Thank you for your time!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

It is because of getLastKnownLocation is returned as null, if you look at the method

https://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String)

This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location. If the provider is currently disabled, null is returned.

Having said that, you should always check for null when using getLastKnownLocation

WenChao
  • 3,586
  • 6
  • 32
  • 46