0

I have an AsyncTask 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 evacRoute but when I run it by just playing I am getting a null pointer exception on if (!evacRoute.getLatLngs().isEmpty()) on my locationlistener below:

/**
 *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 != "" && shelorfuelAddress != null && type.equals("fuelstop")){
                    try {
                        ShowFuelStopAndRoute(shelorfuelAddress);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else if (shelorfuelAddress != "" && shelorfuelAddress != null && type.equals("shelter")){
                    try {
                        ShowShelterAndRoute(shelorfuelAddress);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else if (evacStr != null && 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) {
    }
}

And here is my AsyncTask:

public class AsyncRoute extends AsyncTask<Void, Void, EvacRoute > {

    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.
    }

}

and here is the method that gets called in the AsyncTask method above:

public EvacRoute getEvacuationRoute(String routeName, LatLng currentLocation, String lat, String lon) throws URISyntaxException, ClientProtocolException, IOException, ParserConfigurationException, SAXException{
    evacRouteList = new ArrayList<EvacRoute>();

    evacRoute = new EvacRoute();
    evacRoute.setDestinationName(routeName);
    BufferedReader in = null;
    String page;
    latslngsList = new ArrayList<LatLng>();
    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<NameValuePair> nameValPairs = new ArrayList<NameValuePair>(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 < 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 < 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;
}

Here is the stacktrace:

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)

The line 499 on mapview activity that I am getting the error is if (!evacRoute.getLatLngs().isEmpty()){ 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?

yams
  • 942
  • 6
  • 27
  • 60

1 Answers1

1

Easiest way would be to move

  if (!evacRoute.getLatLngs().isEmpty()){
                    showEvacuationRoute(evacRoute);
  }   

into your onPostExecute method. I'd change it to

if (evacRoute!=null && evacRoute.getLatLngs()!=null && !evacRoute.getLatLngs().isEmpty()){
                    showEvacuationRoute(evacRoute);
}

though.

DigCamara
  • 5,540
  • 4
  • 36
  • 47
  • So would I just comment out that code from the original and paste it into the on postExecute? – yams May 16 '13 at 16:51
  • pretty much, yeah. Since you're setting the LatLngs in the AsyncTask, I figure as soon as it finishes would be the best point to use the method that's actually accessing the information. – DigCamara May 16 '13 at 16:53
  • Ok so now I get it...basically when the doInBackground is complete the on post execute kicks off then right? – yams May 16 '13 at 16:54
  • Yep. Also: while in doInBackground() doesn't allow changes to the user interface, you can do them in onPostExecute – DigCamara May 16 '13 at 16:59
  • Now one more question how difficult would it be to add some type of Progress Bar or Dialog that completes when the onPostExecute is complete? – yams May 16 '13 at 17:03
  • Look at the question here (it pretty much does what you are asking, though (s)he missed the document that tells you how to actually refresh the progress): http://stackoverflow.com/questions/4591878/updating-progress-dialog-in-activity-from-asynctask – DigCamara May 16 '13 at 17:06