Reputation: 1222
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
if(location == null){
longitude = 0.0;
latitude = 0.0;
Log.i("CURRENT LONGITUDE", ""+longitude);
Log.i("CURRENT LATITUDE", ""+latitude);
}
Log.i("CURRENT LONGITUDE", ""+longitude);
Log.i("CURRENT LATITUDE", ""+latitude);
I am getting problem at line "double longitude = location.getLongitude();" which is line 93 in MainActivity;
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): java.lang.NullPointerException
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at com.demo.parsing.MainActivity$1.onClick(MainActivity.java:93)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at android.view.View.performClick(View.java:2364)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at android.view.View.onTouchEvent(View.java:4179)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at android.widget.TextView.onTouchEvent(TextView.java:6540)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at android.view.View.dispatchTouchEvent(View.java:3709)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at android.os.Looper.loop(Looper.java:123)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at android.app.ActivityThread.main(ActivityThread.java:4363)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at java.lang.reflect.Method.invoke(Method.java:521)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-18 16:52:26.928: ERROR/AndroidRuntime(9598): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 110
Reputation: 64399
This is strange:
double longitude = location.getLongitude();
double latitude = location.getLatitude();
if(location == null){
It seems you expect that location can be NULL. But the two lines above that use location to call a member function. If it is NULL, then you will get a nullpointerexception.
in response to your comment: You could easily fix it by changing the order of things:
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER)
double latitude;
double longitude;
if(location == null){
longitude = 0.0;
latitude = 0.0;
Log.i("CURRENT LONGITUDE", ""+longitude);
Log.i("CURRENT LATITUDE", ""+latitude);
}else{
longitude = location.getLongitude();
latitude = location.getLatitude();
}
Edit 2 If you would just take a quick look at the manual: http://developer.android.com/reference/android/location/LocationManager.html
you would have read this: (emph mine)
Returns a Location indicating the data from the last known location fix obtained from the given provider. This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location. If the provider is currently disabled, null is returned.
Upvotes: 3
Reputation: 137312
You check if location is null only after you used it. It might be null during assignment to longtitude /latitude.
Upvotes: 1