Reputation: 1927
I'm getting a null pointer exception from the following line of code:
double longitude = location.getLongitude();
I having an issue figuring out what the problem is.
LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, waypointActivity);
//LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
String locLat = String.valueOf(latitude)+","+String.valueOf(longitude);
Here is the logcat output:
04-30 10:20:54.988: E/AndroidRuntime(1827): FATAL EXCEPTION: main
04-30 10:20:54.988: E/AndroidRuntime(1827): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.LoadedApk.makeApplication(LoadedApk.java:504)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4364)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.ActivityThread.access$1300(ActivityThread.java:141)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.os.Looper.loop(Looper.java:137)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-30 10:20:54.988: E/AndroidRuntime(1827): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 10:20:54.988: E/AndroidRuntime(1827): at java.lang.reflect.Method.invoke(Method.java:511)
04-30 10:20:54.988: E/AndroidRuntime(1827): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-30 10:20:54.988: E/AndroidRuntime(1827): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-30 10:20:54.988: E/AndroidRuntime(1827): at dalvik.system.NativeStart.main(Native Method)
04-30 10:20:54.988: E/AndroidRuntime(1827): Caused by: java.lang.NullPointerException
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:379)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.LoadedApk.getClassLoader(LoadedApk.java:322)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.LoadedApk.makeApplication(LoadedApk.java:496)
04-30 10:20:54.988: E/AndroidRuntime(1827): ... 11 more
04-30 10:21:04.048: E/Trace(1863): error opening trace file: No such file or directory (2)
I can't understand why it thinks my location variable is null. Any suggestions would be appreciated.
Upvotes: 1
Views: 14235
Reputation: 83
This is not a "fix" but more a "workaround":
I have the same issue with my GPS location, though mine seems to be 'the first location returned after reinstalling the app via adb' which makes sense that the provider had not initialised yet. (as reinstalling the app removes any previous known location)
This results in whatever GPS task I was trying to accomplish always failing and returning null the first time it was triggered (all providers), but working as expected when restarted.
I was advised by my supervisor and college to simply workaround the issue by simply beginning gps updates and throwing away the first location, moving immediately onto the next and proceeding from there as normal (detecting and/or handling any null location returned from then onwards as unexpected behaviour)
While this serves the purpose for me, i'd still be interested in how this is actually supposed to be best-practice solved?
Hope this helps someone continue working in the meantime.
Upvotes: 0
Reputation: 2681
The LocationManager.GPS_PROVIDER
(accessing location using phone's GPS) takes some time to access location, due to which you are getting location as null from locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
Make your location object global and in your waypointActivity initialize this object in the implemented onLocationChanged
method of LocationListener
Something like this:-
@Override
public void onLocationChanged(Location location) {
this.location = location;
}
then check if the location is not null before using it as:-
if (location!=null){
double longitude = location.getLongitude();
double latitude = location.getLatitude();
String locLat = String.valueOf(latitude)+","+String.valueOf(longitude);
}
I would recommend you use LocationManager.NETWORK_PROVIDER
insted, because it gives the result much faster than GPS_PROVIDER
.
Upvotes: 0
Reputation: 5568
The documentation states:
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.
Since you're getting null for GPS location, you probably have not turned on your GPS or your phone has no GPS.
Change your code to
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location!=null){
double longitude = location.getLongitude();
double latitude = location.getLatitude();
String locLat = String.valueOf(latitude)+","+String.valueOf(longitude);
}
Upvotes: 5