title: Android Location DistanceTo null
body: <pre><code>package com.competitivegamingaudio;
  import android.app.Activity;
  import android.content.Context;
  import android.location.*;
  import android.os.Bundle;
  import android.util.Log;
  import android.widget.*;
  import com.google.android.maps.*;;
  
  public class FindMyFriendsActivity extends Activity {
  
  
  private static final String TAG = "FindMyFriendsActivity";
  public TextView LocationLabel;
  public TextView DistanceLabel;
  public MapView myMapView;
  public Location oldlocation;
  public Location currentlocation;
  
  @Override
  public void onResume()
      {
      super.onResume();
      LocationManager locationManager=(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
       locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
      }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      LocationLabel=(TextView) findViewById(R.id.LocationLabel);
      DistanceLabel=(TextView)findViewById(R.id.DistanceLabel);
  
  
  
  }
  
  LocationListener locationListener =new LocationListener()
  {
  public void onLocationChanged (Location location)
      {
      currentlocation=new Location(location);
      LocationLabel.setText(location.toString());
      oldlocation=new Location(location);
      DistanceLabel.setText("Distance: "+calculatedistance(currentlocation,oldlocation));
  
      }
  public String calculatedistance(Location a, Location b)
      {
      a.distanceTo(b);
      return ""+a;
      }
  public void onStatusChanged(String provider, int status, Bundle extras)
      {}
  public void onProviderEnabled(String provider)
      {
  
      }
  public void onProviderDisabled(String provider)
  {}
  
  };
  
  }
  </code></pre>
  
  <p>I'm having trouble getting this code to work properly. I want to be able to calculate the distance between two Locations. The initial location that's being passed to the onLocationChanged method is working (which proves I have the right conditions), because when I do <code>LocationLabel.setText(location.toString());</code> that works properly without getting any null pointer exceptions. However, when the <code>calculatedistance</code> method is called in line 49, the <code>a.distanceTo(b);</code> line fails on line 54. Any idea why the distanceTo method keeps failing?</p>
  
  <p>Error Message:</p>
  
  <pre><code>01-09 20:11:58.085: ERROR/AndroidRuntime(9716): java.lang.NullPointerException
  01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at com.competitivegamingaudio.FindMyFriendsActivity$1.onLocationChanged(FindMyFriendsActivity.java:47)
  01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:227)
  01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:160)
  01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:176)
  01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at android.os.Handler.dispatchMessage(Handler.java:99)
  01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at android.os.Looper.loop(Looper.java:130)
  01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at android.app.ActivityThread.main(ActivityThread.java:3821)
  01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at java.lang.reflect.Method.invokeNative(Native Method)
  01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at java.lang.reflect.Method.invoke(Method.java:507)
  01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  01-09 20:11:58.085: ERROR/AndroidRuntime(9716):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>main.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  &gt;
  &lt;TextView  
  android:id="@+id/LocationLabel"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="@string/hello"
  /&gt; 
  &lt;TextView  
      android:id="@+id/DistanceLabel"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="@string/hello"
  /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>