I am trying to find out current latitude and longitude of android device but I am getting java.lang.nullpointerexception and app is force closing.even though i get refrence from here and used the code from the answer which was accepted as correct: How to get Android GPS location my code is:
try{
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(bestProvider);
double latit=location.getLatitude();
double longit=location.getLongitude();
Toast.makeText(getApplicationContext(),"Your Location is:\nLatitude:\t"+latit+"\nLongitude:\t"+longit,Toast.LENGTH_LONG).show();
}catch(Exception ex){
Toast.makeText(getApplicationContext(), "Error:"+ex,Toast.LENGTH_LONG).show();
}