Error Invoke Virtual Method 'Double Android.Location.Location.Getlatitude()' on a Null Object Reference

Error invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference

As Jon Skeet mentioned in the comments, the getLastKnownLocation() method can and will return null. The main problem is that it doesn't prompt a request to the OS for a new location lock, instead it just checks if there was a last known location from some other app's location request. If no other app had recently made a location request, then you get a null location returned to you.

The only way to guarantee that you actually get a location is to request one, and this is done with a call to requestLocationUpdates().

The location passed into the onLocationChanged() callback method will not be null, since the callback only occurs on a successful location lock.

Just to note, the entire time your app is registered for location updates, it will be causing extra battery drain, so be sure to un-register for location updates as soon as possible. Here it looks like you can un-register as soon as the first location comes in.

Also, you might want to consider showing a progress dialog in this Activity while it waits for a location lock in order to give the user some feedback that the app is waiting on something.

Here is the general structure of what your code should look like:

public class MainActivity extends Activity
implements LocationListener {

Intent intentThatCalled;
public double latitude;
public double longitude;
public LocationManager locationManager;
public Criteria criteria;
public String bestProvider;

String voice2text; //added

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

intentThatCalled = getIntent();
voice2text = intentThatCalled.getStringExtra("v2txt");
getLocation();
}

public static boolean isLocationEnabled(Context context)
{
//...............
return true;
}

protected void getLocation() {
if (isLocationEnabled(MainActivity.this)) {
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
bestProvider = String.valueOf(locationManager.getBestProvider(criteria, true)).toString();

//You can still do this if you like, you might get lucky:
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
Log.e("TAG", "GPS is on");
latitude = location.getLatitude();
longitude = location.getLongitude();
Toast.makeText(MainActivity.this, "latitude:" + latitude + " longitude:" + longitude, Toast.LENGTH_SHORT).show();
searchNearestPlace(voice2text);
}
else{
//This is what you need:
locationManager.requestLocationUpdates(bestProvider, 1000, 0, this);
}
}
else
{
//prompt user to enable location....
//.................
}
}

@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);

}

@Override
public void onLocationChanged(Location location) {
//Hey, a non null location! Sweet!

//remove location callback:
locationManager.removeUpdates(this);

//open the map:
latitude = location.getLatitude();
longitude = location.getLongitude();
Toast.makeText(MainActivity.this, "latitude:" + latitude + " longitude:" + longitude, Toast.LENGTH_SHORT).show();
searchNearestPlace(voice2text);
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

public void searchNearestPlace(String v2txt) {
//.....
}
}

Getting error java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()'

mLastLocation can be effectively null. You've made a check at the top of the method but not after...

Try this :

btnRequest.setOnClickListener(new View.OnClickListener() {@
Override
public void onClick(View view) {
if (mLastLocation != null) {
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
String userId = FirebaseAuth.getInstance().getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("customerRequest");
GeoFire geoFire = new GeoFire(ref);
geoFire.setLocation(userId, new GeoLocation(latitude, longitude), new GeoFire.CompletionListener() {@
Override
public void onComplete(String key, DatabaseError error) {}
});
pickupLocation = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions()
.position(pickupLocation)
.title("Pickup Here"));
btnRequest.setText("Getting your rider...");
}
}
});

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

You forget to initialize Location, just add this :

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);

on onCreate() method:

after that getting lat&lng from location :

double lat = location.getLatitude();
double lng = location.getLongitude();
LatLng latLng = new LatLng(lat, lng);

Happy coding!!

Location.getLatitude() on a null object reference

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

The location is null, it has not been initialised.

Check that location is not null:

if(location!= null){
double latitude = location.getLatitude();
double longitude = location.getLongitude();
}

Also you should be wrapping all your location services code into a try and catch and be checking for null pointer references and connections, GPS providers etc. Have a look here BasicLocationSample.

Attempt invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference

The exception you're seeing in logcat points to a problem in the onCreate method of your activity.

The interesting part is this:
java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference. It's telling you that whatever object you're calling getLatitude() on is null.

Looking at the body of the method, it's only possible on this line: LatLng koordinat = new LatLng(mLocation.getLatitude(), mLocation.getLongitude());. Therefore you can tell that the crash is caused by mLocation being null.

You're initializing mLocation like this: mLocationManager.getLastKnownLocation(PROVİDER);.
The documentation tells you it's possible to get a null return value if the provider is disabled.

That should answer the "why". Now you can think about the best way to work around this for your application. Can you try another provider? If none of the providers are enabled, can you default to a generic location? Do you want to show an error message instructing the user to enable location services and provide a way to try again?



Related Topics



Leave a reply



Submit