1

I have this code to get Location. The problem is not in permission.

   public Location getLocation() {
    Location location = null;
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

    }
    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    return location;
}

This is how I get location.

    lat = location.getLatitude();
    lng = location.getLongitude();

    LatLng myLocation = new LatLng(lat, lng);

Error message :

java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference

I got lost in this. Please what's wrong?

MELI
  • 21
  • 2
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Selvin Jan 29 '16 at 14:19
  • 2
    As documentation said `LocationManager.getLastKnownLocation(...)` may return null ... and it returns null... – Selvin Jan 29 '16 at 14:19

0 Answers0