0

I have created an Android App which opens a Map Fragment and is to create 2 markers:

Marker 1 is the current location (which i believe may be the cause of the problem)

Marker 2 which gets the coords(LatLng) passed from a previous Activity

My problem is that when I run the app for the first time it crashes ... stops responding I have made sure all the permissions and manifest file is correct. But i have made the following observation : After the app crashes if i open Google Maps close everything and run the app again it works fine.

The following error message is generated:

2020-07-16 09:51:16.363 3833-3833/com.notbytes.barcodereader E/AndroidRuntime: FATAL EXCEPTION: main Process: com.notbytes.barcodereader, PID: 3833 java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference at com.notbytes.barcodereader.MainActivity2$4.onSuccess(MainActivity2.java:197) at com.notbytes.barcodereader.MainActivity2$4.onSuccess(MainActivity2.java:192) at com.google.android.gms.tasks.zzn.run(Unknown Source) 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:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

Any help or assistance will be greatly appreciated.

TIA

ShahiM
  • 3,179
  • 1
  • 33
  • 58
  • 1
    Welcome to SO. It would be helpful if you could add some relevant parts of the code. It looks like its crashing when you try to get the Lattitude. – ShahiM Jul 16 '20 at 08:02

1 Answers1

1

Sometimes the getLastLocation() method might return null, because the fused location provider doesn't update the location cache after the device is restarted. If getLastLocation() returns null unexpectedly:

  1. Location is turned off in the device settings. The result could be null even if the last location was previously retrieved because disabling location also clears the cache.
  2. The device never recorded its location, which could be the case of a new device or a device that has been restored to factory settings
  3. Google Play services on the device has restarted, and there is no active Fused Location Provider client that has requested location after the services restarted. To avoid this situation you can create a new client and request location updates yourself. For more information, see Receive Location updates.

But in your case, I guess you have not added any check before getting latitude and longitude.

Below is a sample code of how to get and set location.

mFusedLocationClient.getLastLocation().addOnSuccessListener(
           new OnSuccessListener<Location>() {
    @Override
    public void onSuccess(Location location) {

//I added below if-statement to prevent my app from crash or nullpointerException.

       if (location != null) {
           mLastLocation = location;
           mLocationTextView.setText(
                   getString(R.string.location_text,
                           mLastLocation.getLatitude(),
                           mLastLocation.getLongitude(),
                           mLastLocation.getTime()));
       } else {
           mLocationTextView.setText(R.string.no_location);
       }
    }

if this does not solve your problem then please try to add your code as well.

Shoaib Kakal
  • 1,090
  • 2
  • 16
  • 32
  • I added a the line to handle the NOT NULL, which will then pass the coordinates, but i need to handle the NULL. Ideally, I would like to update until a coordinate is returned. What could be added in the ELSE so that I ensure that a cordinate is passed rather than display no_location?? Your help is really appreciated !!! – TheRealMrP Jul 16 '20 at 11:24
  • The following solved the issue ... but im not sure why since there should be no last known cached location on reboot.. – TheRealMrP Jul 16 '20 at 12:02
  • public void callLocationUpdates(){ .....{ @Override public void onSuccess(Location location) { if(location!=null) { testMycurr(location.getLatitude(), location.getLongitude()) } else{ callLocationUpdates(); fusedLocationProviderClient.requestLocationUpdates(locationRequest,new LocationCallback(), Looper.getMainLooper()); } } }); } – TheRealMrP Jul 16 '20 at 12:03
  • I have updated my answer. – Shoaib Kakal Jul 16 '20 at 12:27