0

the following code for deriving longitude and latitude gives me a NPE

Initial Declarations:

double longitude, latitude;
Location location;
LocationManager manager;

code:

final LocationListener locationListener = new LocationListener(){

                        @Override
                        public void onLocationChanged(Location arg0) {
                            // TODO Auto-generated method stub
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }

                        @Override
                        public void onProviderDisabled(String arg0) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onProviderEnabled(String arg0) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onStatusChanged(String arg0, int arg1,
                                Bundle arg2) {
                            // TODO Auto-generated method stub

                        }

                    };
                    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
                    location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    longitude = location.getLongitude();
                    latitude = location.getLatitude();
                    Toast.makeText(getApplicationContext(), "lat and lon"+latitude+" "+longitude, Toast.LENGTH_LONG).show();

The NPE occurs in

longitude = location.getLongitude(); //Line 91

The logCat:

09-01 09:58:27.689: D/AndroidRuntime(3073): Shutting down VM
09-01 09:58:27.689: W/dalvikvm(3073): threadid=1: thread exiting with uncaught exception (group=0x40df98a8)
09-01 09:58:27.749: E/AndroidRuntime(3073): FATAL EXCEPTION: main
09-01 09:58:27.749: E/AndroidRuntime(3073): java.lang.NullPointerException
09-01 09:58:27.749: E/AndroidRuntime(3073):     at com.vaw.selfhelp.SmsActivity$2.onClick(SmsActivity.java:91)
09-01 09:58:27.749: E/AndroidRuntime(3073):     at android.view.View.performClick(View.java:4091)
09-01 09:58:27.749: E/AndroidRuntime(3073):     at android.view.View$PerformClick.run(View.java:17072)
09-01 09:58:27.749: E/AndroidRuntime(3073):     at android.os.Handler.handleCallback(Handler.java:615)
09-01 09:58:27.749: E/AndroidRuntime(3073):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 09:58:27.749: E/AndroidRuntime(3073):     at android.os.Looper.loop(Looper.java:153)
09-01 09:58:27.749: E/AndroidRuntime(3073):     at android.app.ActivityThread.main(ActivityThread.java:4987)
09-01 09:58:27.749: E/AndroidRuntime(3073):     at java.lang.reflect.Method.invokeNative(Native Method)
09-01 09:58:27.749: E/AndroidRuntime(3073):     at java.lang.reflect.Method.invoke(Method.java:511)
09-01 09:58:27.749: E/AndroidRuntime(3073):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
09-01 09:58:27.749: E/AndroidRuntime(3073):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
09-01 09:58:27.749: E/AndroidRuntime(3073):     at dalvik.system.NativeStart.main(Native Method)

The following permission are used:

<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74
  • @MT8 is it always necessary to have the device out ? When the application is deployed, there's no telling where people will be at. – Rakeeb Rajbhandari Sep 01 '13 at 04:36
  • Yaa but in GPS signals might not be good inside the building. Also if you place if around tall buildings then then reception might be poor. That's why you might need to use more than one providers, if available. – Shobhit Puri Sep 01 '13 at 04:55
  • @ShobhitPuri how do i do that ? – Rakeeb Rajbhandari Sep 01 '13 at 04:57
  • 1
    Check http://stackoverflow.com/questions/2919358/android-get-location-from-best-provider-available and http://stackoverflow.com/questions/2504429/android-get-current-location-from-best-available-provider . You can define a `Criteria` and the based on that Location provider which satisfy the criteria will be chosen from amongst WI-FI, Cell tower and GPS. – Shobhit Puri Sep 01 '13 at 05:10

1 Answers1

2

You are using the variable location that is not being initialized. Instead use the arg0 variable

@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub
    latitude = arg0.getLatitude();
    longitude = arg0.getLongitude();
}

Or you can just initialize your location variable first:

@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub
    location=arg0;
    latitude = location.getLatitude();
    longitude = location.getLongitude();
}
CoderDecoder
  • 445
  • 4
  • 18
Raúl Juárez
  • 2,129
  • 1
  • 19
  • 17