Question

I am a newbie to Android. I have developed an app in which I am required to track a user's location using either gps, internet or using cellid.I am using 2 Activities(MainActivity and SavelocationsActivity implements LocationListener),a Service(Ser) and a Broadcast Receiver(k).

When I click a Button in the MainActivity it was required to go to SaveLocationsActivity. I used to get that a few days before but now the lattitude and longitude values have become null and I am getting the following errors i.e:

null pointer exception.

As I am a newbie please explain me what to do and where I am going wrong step by step.Thank you in advance. I am posting the code and error log below.

SaveLocationsActivity

 public class SaveLocationsActivity extends Activity implements LocationListener{
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.savelocations);
   locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
    isNetworkEnabled = locationManager
    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    //Toast.makeText(this,"in activity"+isNetworkEnabled ,Toast.LENGTH_LONG).show();
          isGPSEnabled =       locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    if(isNetworkEnabled==true && isGPSEnabled==false)
    {
        getLocation(LocationManager.NETWORK_PROVIDER);

    }
    else if(isGPSEnabled==true && isNetworkEnabled==false)
    {
        getLocation(LocationManager.GPS_PROVIDER);
    }
    else if(isGPSEnabled==true && isNetworkEnabled==true )
    {
        getLocation(LocationManager.GPS_PROVIDER);
    }
    private Location getLocation(String provider) {
    // TODO Auto-generated method stub
    if (locationManager.isProviderEnabled(provider)) {
        locationManager.requestLocationUpdates(provider,
                6000, 10, this);
        if (locationManager != null) {
            location = locationManager.getLastKnownLocation(provider);
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
              final String t5 = String.valueOf(latitude);
             final String t6 = String.valueOf(longitude);

            e5.setText(t5);
            e6.setText(t6);

            savelocation.put("t5",t5);
            savelocation.put("t6",t6);

            return location;
        }
    }
    return location;
}
@Override
public void onLocationChanged(Location l) {
    // TODO Auto-generated method stub
    double latitude = l.getLatitude();
    double longitude = l.getLongitude();
    final String t5 = String.valueOf(latitude);
    final String t6 = String.valueOf(longitude);
    //Toast.makeText(SaveLocationsActivity.this,""+t5,Toast.LENGTH_LONG).show();
    e5.setText(t5);
    e6.setText(t6);
}

Error logs:

  03-21 23:34:50.309: E/AndroidRuntime(15368): FATAL EXCEPTION: main
  03-21 23:34:50.309: E/AndroidRuntime(15368): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.broadcast/com.example.broadcast.SaveLocationsActivity}: java.lang.NullPointerException
 03-21 23:34:50.309: E/AndroidRuntime(15368):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
 03-21 23:34:50.309: E/AndroidRuntime(15368):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
 03-21 23:34:50.309: E/AndroidRuntime(15368):   at android.app.ActivityThread.access$700(ActivityThread.java:143)
 03-21 23:34:50.309: E/AndroidRuntime(15368):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
 03-21 23:34:50.309: E/AndroidRuntime(15368):   at android.os.Handler.dispatchMessage(Handler.java:99)
 03-21 23:34:50.309: E/AndroidRuntime(15368):   at android.os.Looper.loop(Looper.java:137)
 03-21 23:34:50.309: E/AndroidRuntime(15368):   at android.app.ActivityThread.main(ActivityThread.java:4960)
 03-21 23:34:50.309: E/AndroidRuntime(15368):   at java.lang.reflect.Method.invokeNative(Native Method)
 03-21 23:34:50.309: E/AndroidRuntime(15368):   at java.lang.reflect.Method.invoke(Method.java:511)
  03-21 23:34:50.309: E/AndroidRuntime(15368):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
 03-21 23:34:50.309: E/AndroidRuntime(15368):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
 03-21 23:34:50.309: E/AndroidRuntime(15368):   at dalvik.system.NativeStart.main(Native Method)
 03-21 23:34:50.309: E/AndroidRuntime(15368): Caused by: java.lang.NullPointerException
 03-21 23:34:50.309: E/AndroidRuntime(15368):   at com.example.broadcast.SaveLocationsActivity.getLocation(SaveLocationsActivity.java:209)
 03-21 23:34:50.309: E/AndroidRuntime(15368):   at com.example.broadcast.SaveLocationsActivity.onCreate(SaveLocationsActivity.java:100)
 03-21 23:34:50.309: E/AndroidRuntime(15368):   at android.app.Activity.performCreate(Activity.java:5203)
 03-21 23:34:50.309: E/AndroidRuntime(15368):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
 03-21 23:34:50.309: E/AndroidRuntime(15368):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
 03-21 23:34:50.309: E/AndroidRuntime(15368):   ... 11 more
Was it helpful?

Solution

In the method getLocation() the problem is probably here:

if (locationManager != null) {
    location = locationManager.getLastKnownLocation(provider);
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();  

because getLastKnownLocation can return null if the previous location is unknown. So by calling location.getLatitude() you incur in a NullPointerException.
To avoid this you can check the returned value for null like:

if (locationManager != null) {
    location = locationManager.getLastKnownLocation(provider);
    if(location!=null)
         //...etc...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top