0

I have a method to show on the map the current position of the device. I am using FusedLocationProviderClient because it seems to be the most accurate.

On currentLocation.getLatitude() I get the warning:

Method invocation may produce java.Lang.NullPointerException

How can I avoid this risk? And why it is related only to currentLocation.getLatitude() and not to currentLocation.getLongitude()?

private void getDeviceLocation(){
    Log.d(TAG, "getDeviceLocation: getting the devices current location");

    FusedLocationProviderClient mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

    try{
        if(mLocationPermissionsGranted){

            final Task location = mFusedLocationProviderClient.getLastLocation();

            location.addOnCompleteListener(task -> {
                if(task.isSuccessful()){
                    Log.d(TAG, "onComplete: found location!");

                    Location currentLocation = (Location) task.getResult();
                    LatLng latLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());

                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, DEFAULT_ZOOM));

                }else{
                    Log.d(TAG, "onComplete: current location is null");
                }
            });
        }
    }catch (SecurityException e){
        Log.e(TAG, "getDeviceLocation: SecurityException: " + e.getMessage() );
    }
}
RKRK
  • 1,284
  • 5
  • 14
  • 18
Stefano
  • 209
  • 2
  • 10
  • 34

2 Answers2

0

IDEs can be weird or misinterpreted when they display information regarding your code but I assure you it applies to getLongitude() and any other invocation of instance methods or instance members because the location can be null.

public Task getLastLocation ()
Returns the best most recent location currently available.

If a location is not available, which should happen very rarely, null will be returned. The best accuracy available while respecting the location permissions will be returned.

This method provides a simplified way to get location. It is particularly well suited for applications that do not require an accurate location and that do not want to maintain extra logic for location updates.

https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient.html#getLastLocation()

So even if the task is successful, the result of the task can be null. All this means is that you need to check if location is null before using it.

Red
  • 16
  • 3
0

As Red has indicated, calling any other methods on the currentLocation reference will throw a NullPointerException if the underlying object is null, which is a possible result of getLastLocation(). The reason the IDE only warns you about the call to getLatitude() is because it is the first call to currentLocation that dereferences it. If that call throws the exception, then the other method calls wouldn't even be made. If it doesn't throw the exception, then the others won't either. Therefore, the IDE doesn't need to warn you about them because the object being null ultimately won't matter at that point.

Hei2
  • 44
  • 4