I am using the following piece of code in my Android application to find my mobile device's current location :
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Double longitude = location.getLongitude();
Double latitude = location.getLatitude();
Log.i("LATITUDE", latitude.toString());
Log.i("LONGITUTDE", longitude.toString());
Up to now, it was working fine, and it was giving me the right coordinates of my location. But before a while and without making any change at all to my code, it started giving me the following error :
java.lang.RuntimeException: Unable to start activity ComponentInfo{di.uoa.gr.e_commerce/di.uoa.gr.e_commerce.FirstResultsActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769) at android.app.ActivityThread.access$900(ActivityThread.java:177) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5910) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
I do have my WiFi connection open, as well as my Location. Can anyone figure out what could be going wrong?