title: Null Pointer after AsyncTask is added.
body: <p>I have an <code>AsyncTask</code> that I have added to a web service call on my android app. When I step through the code in debug mode I do not get the null pointer on my variable called <code>evacRoute</code> but when I run it by just playing I am getting a null pointer exception on <code>if (!evacRoute.getLatLngs().isEmpty())</code> on my locationlistener below: </p>
  
  <pre><code>/**
   *Mylocationlistener class will give the current GPS location 
   *with the help of Location Listener interface 
   */
  private class PointLocationListener implements LocationListener {
      private boolean zoomed = false;
      @Override
      public void onLocationChanged(Location location) {
  
          if (location != null) {
              // ---Get current location latitude, longitude---
  
              Log.d("LOCATION CHANGED", location.getLatitude() + "");
              Log.d("LOCATION CHANGED", location.getLongitude() + "");
              latitude = 36.857648;
              longitude = -76.170959;
  
  
              currentLocation = new LatLng(latitude, longitude);
              //currentLatLng = new LatLng(latitude, longitude);
              Marker currentLocationMarker = map.addMarker(new MarkerOptions().position(currentLocation).title("Current Location"));
              // Move the camera instantly to current location with a zoom of 15.
              map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 15));
              // Zoom in, animating the camera.
  
              if (evacName != null){
                String latStr = String.valueOf(latitude);
                String longStr = String.valueOf(longitude);
  
  
                AsyncRoute asyncRoute = new AsyncRoute(evacName, currentLocation, latStr, longStr);
                asyncRoute.execute();
  
                System.out.println();
              }
              if (!zoomed) {
                  map.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);
                  zoomed = true;
              }
              if (!firstPass){
                  currentLocationMarker.remove();
              }
              firstPass = false;
              Toast.makeText(MapViewActivity.this,"Latitude = "+
                      latitude + "" +"Longitude = "+ longitude,
                      Toast.LENGTH_LONG).show();
              evacRoute = DALController.sharedInstance().getSelectedEvacRoute();
              if (fuelStopBundle != null){
                  String shelorfuelAddress = fuelStopBundle.getString("key");
                  String type = fuelStopBundle.getString("type");
                  String evacStr = fuelStopBundle.getString("evacObject");
                  if (shelorfuelAddress != "" &amp;&amp; shelorfuelAddress != null &amp;&amp; type.equals("fuelstop")){
                      try {
                          ShowFuelStopAndRoute(shelorfuelAddress);
                      } catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                  } else if (shelorfuelAddress != "" &amp;&amp; shelorfuelAddress != null &amp;&amp; type.equals("shelter")){
                      try {
                          ShowShelterAndRoute(shelorfuelAddress);
                      } catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                  } else if (evacStr != null &amp;&amp; evacRoute != null){
                      if (!evacRoute.getLatLngs().isEmpty()){
                          showEvacuationRoute(evacRoute);
                      }   
                  }
  
              } else {
  
              }
          } else {
  
          }
      }
  
      @Override
      public void onProviderDisabled(String provider) {
      }
  
      @Override
      public void onProviderEnabled(String provider) {
      }
  
      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
      }
  }
  </code></pre>
  
  <p>And here is my AsyncTask:</p>
  
  <pre><code>public class AsyncRoute extends AsyncTask&lt;Void, Void, EvacRoute &gt; {
  
      public String routeName;
      public LatLng currentLocation;
      public String lat;
      public String lon;
  
      public AsyncRoute (String routeName, LatLng currentLocation, String lat, String lon){
      this.routeName = routeName;
      this.currentLocation = currentLocation;
      this.lat = lat;
      this.lon = lon;    
      }
  
      @Override
      protected EvacRoute  doInBackground(Void... params) {
          try {
              return DALController.sharedInstance().getEvacuationRoute(routeName, currentLocation, lat, lon);
          } catch (ClientProtocolException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (URISyntaxException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (ParserConfigurationException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (SAXException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          return null;
      }
  
      @Override
      protected void onPostExecute(EvacRoute  result) {
          // Function finished and value has returned.
      }
  
  }
  </code></pre>
  
  <p>and here is the method that gets called in the AsyncTask method above:</p>
  
  <pre><code>public EvacRoute getEvacuationRoute(String routeName, LatLng currentLocation, String lat, String lon) throws URISyntaxException, ClientProtocolException, IOException, ParserConfigurationException, SAXException{
      evacRouteList = new ArrayList&lt;EvacRoute&gt;();
  
      evacRoute = new EvacRoute();
      evacRoute.setDestinationName(routeName);
      BufferedReader in = null;
      String page;
      latslngsList = new ArrayList&lt;LatLng&gt;();
      try {
  
          latslngsList.add(currentLocation);
          HttpClient client = new DefaultHttpClient();
          HttpPost request = new HttpPost();
          request.setURI(new URI("http://heed-dev01.vmasc.odu.edu:53556/heed_service2.asmx/get_route_info_from_lat_lon"));
          //Add The parameters.  The asmx webservice requires a double but gets posted as a string in a text field
          List&lt;NameValuePair&gt; nameValPairs = new ArrayList&lt;NameValuePair&gt;(2);
          nameValPairs.add(new BasicNameValuePair("Route_Name", routeName));
          nameValPairs.add(new BasicNameValuePair("In_Lat", lat));
          nameValPairs.add(new BasicNameValuePair("In_Lon", lon));
          request.setEntity(new UrlEncodedFormEntity(nameValPairs));
  
          HttpResponse response = client.execute(request);
          in = new BufferedReader
          (new InputStreamReader(response.getEntity().getContent()));
          StringBuffer sb = new StringBuffer("");
          String line = "";
          String NL = System.getProperty("line.separator");
          while ((line = in.readLine()) != null) {
              sb.append(line + NL);
  
          }
          in.close();
          page = sb.toString();
  
  
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();
          Document doc = db.parse(new InputSource(new StringReader(page)));
          // normalize the document
          doc.getDocumentElement().normalize();
          // get the root node
          NodeList nodeList = doc.getElementsByTagName("simple_ll_waypoint");
          double latitude = 0;
          double longitude= 0;
          // the  node has three child nodes
          for (int n = 0; n &lt; nodeList.getLength(); n++) {
              String latString = "";
              String longString = "";
  
              Node node=nodeList.item(n);
              String upperNode = node.getNodeName();
              StringBuilder addressStrBlder = new StringBuilder();
              for (int i = 0; i &lt; node.getChildNodes().getLength(); i++) {
                  Node temp=node.getChildNodes().item(i);
                  String nodeName = temp.getNodeName();
                  String nodevalue = temp.getNodeValue();
                  if(temp.getNodeName().equalsIgnoreCase("Lat")){
                      latString = temp.getTextContent();
                      latitude = Double.parseDouble(latString);
  
                  } else if(temp.getNodeName().equalsIgnoreCase("Lon")){
                      longString = temp.getTextContent();
                      longitude = Double.parseDouble(longString);
                      LatLng latlng = new LatLng(latitude, longitude);
                      latslngsList.add(latlng);
                  } 
  
              }
              //Log.e("Fuel Stop", fuelStop.toString());
          }
  
          //System.out.println(page); 
          } catch (Exception E) {  
              E.printStackTrace();  
          } 
      finally {
          if (in != null) {
              try {
                  in.close();
                  } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      evacRoute.setLatLngList(latslngsList);
      evacRouteList.add(evacRoute);
      return evacRoute;
  }
  </code></pre>
  
  <p>Here is the stacktrace: </p>
  
  <pre><code>05-16 09:48:31.917: E/AndroidRuntime(3649): FATAL EXCEPTION: main
  05-16 09:48:31.917: E/AndroidRuntime(3649): java.lang.NullPointerException
  05-16 09:48:31.917: E/AndroidRuntime(3649):     at com.eema.MapViewActivity$PointLocationListener.onLocationChanged(MapViewActivity.java:499)
  05-16 09:48:31.917: E/AndroidRuntime(3649):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:255)
  05-16 09:48:31.917: E/AndroidRuntime(3649):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:184)
  05-16 09:48:31.917: E/AndroidRuntime(3649):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:200)
  05-16 09:48:31.917: E/AndroidRuntime(3649):     at android.os.Handler.dispatchMessage(Handler.java:99)
  05-16 09:48:31.917: E/AndroidRuntime(3649):     at android.os.Looper.loop(Looper.java:137)
  05-16 09:48:31.917: E/AndroidRuntime(3649):     at android.app.ActivityThread.main(ActivityThread.java:5041)
  05-16 09:48:31.917: E/AndroidRuntime(3649):     at java.lang.reflect.Method.invokeNative(Native Method)    
  05-16 09:48:31.917: E/AndroidRuntime(3649):     at java.lang.reflect.Method.invoke(Method.java:511)
  05-16 09:48:31.917: E/AndroidRuntime(3649):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  05-16 09:48:31.917: E/AndroidRuntime(3649):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  05-16 09:48:31.917: E/AndroidRuntime(3649):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>The line 499 on mapview activity that I am getting the error is <code>if (!evacRoute.getLatLngs().isEmpty()){</code> in the code above in the PointLocationListener because evacRoute.getLatsLngs() is null.  Can I move some of the code into the onPostExecute and if so how?</p>