NullPointerException when using GMapsV2Direction c

2019-05-26 06:35发布

I've been trying to fix this run-time error for the last few days to no avail, not sure why it's throwing a NullPointerException when it creates the NodeList when the getDirection method is called. I'm very new to Android programming so if anyone can offer me a little guidance it would be much appreciated, thank you!

EDIT: Judging by the debugger the LatLng co-ordinates and the direction type is being passed through the getDocument() method, but a document isn't being returned from the getDocument() method.

public Document getDocument(LatLng start, LatLng end, String mode) {
    String url = "http://maps.googleapis.com/maps/api/directions/xml?" 
            + "origin=" + start.latitude + "," + start.longitude  
            + "&destination=" + end.latitude + "," + end.longitude 
            + "&sensor=false&units=metric&mode=driving";

    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse response = httpClient.execute(httpPost, localContext);
        InputStream in = response.getEntity().getContent();
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(in);
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

.

public ArrayList<LatLng> getDirection (Document doc) {
    NodeList nl1, nl2, nl3, nl4;
    ArrayList<LatLng> listGeopoints = new ArrayList<LatLng>();
    double time=0;
    nl1 = doc.getElementsByTagName("step");
    if (nl1.getLength() > 0) {
        for (int i = 0; i < nl1.getLength(); i++) {
        double t=0;
            Node node1 = nl1.item(i);
            nl2 = node1.getChildNodes();

            Node locationNode = nl2.item(getNodeIndex(nl2, "start_location"));
            nl3 = locationNode.getChildNodes();
            Node latNode = nl3.item(getNodeIndex(nl3, "lat"));
            double lat = Double.parseDouble(latNode.getTextContent());
            Node lngNode = nl3.item(getNodeIndex(nl3, "lng"));
            double lng = Double.parseDouble(lngNode.getTextContent());
            listGeopoints.add(new LatLng(lat, lng));

            locationNode = nl2.item(getNodeIndex(nl2, "polyline"));
            nl3 = locationNode.getChildNodes();
            latNode = nl3.item(getNodeIndex(nl3, "points"));
            ArrayList<LatLng> arr = decodePoly(latNode.getTextContent());
            for(int j = 0 ; j < arr.size() ; j++) {
                listGeopoints.add(new LatLng(arr.get(j).latitude, arr.get(j).longitude));
            }

            locationNode = nl2.item(getNodeIndex(nl2, "end_location"));
            nl3 = locationNode.getChildNodes();
            latNode = nl3.item(getNodeIndex(nl3, "lat"));
            lat = Double.parseDouble(latNode.getTextContent());
            lngNode = nl3.item(getNodeIndex(nl3, "lng"));
            lng = Double.parseDouble(lngNode.getTextContent());
            listGeopoints.add(new LatLng(lat, lng));


            locationNode = nl2.item(getNodeIndex(nl2, "duration"));
            nl4 = locationNode.getChildNodes();
            Node node2 = nl4.item(getNodeIndex(nl4, "value"));
            t = Double.parseDouble(node2.getTextContent());
            time=time+t;
        }
        Log.i("duration", String.valueOf(time));
    }

    return listGeopoints;
}

Here is the function I'm using to call the getDirection method:

public void onClick_GetDirections(View v){
MyGPSToolDirections md = new MyGPSToolDirections();

LatLng fromPosition = new LatLng(13.68714, 100.53525);
LatLng toPosition = new LatLng(13.68366, 100.53900);

Document doc1 = md.getDocument(fromPosition, toPosition,     MyGPSToolDirections.MODE_DRIVING);

ArrayList<LatLng> directionPoint = md.getDirection(doc1);
PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);

for(int i = 0 ; i < directionPoint.size() ; i++) {          
rectLine.add(directionPoint.get(i));

GPSToolMap.addPolyline(rectLine);
} 

Stack trace:

08-07 17:14:31.682: E/AndroidRuntime(2201): FATAL EXCEPTION: main
08-07 17:14:31.682: E/AndroidRuntime(2201): java.lang.IllegalStateException: Could not execute method of the activity
08-07 17:14:31.682: E/AndroidRuntime(2201):     at android.view.View$1.onClick(View.java:3599)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at android.view.View.performClick(View.java:4204)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at android.view.View$PerformClick.run(View.java:17355)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at android.os.Handler.handleCallback(Handler.java:725)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at android.os.Looper.loop(Looper.java:137)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at java.lang.reflect.Method.invokeNative(Native Method)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at java.lang.reflect.Method.invoke(Method.java:511)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at dalvik.system.NativeStart.main(Native Method)
08-07 17:14:31.682: E/AndroidRuntime(2201): Caused by: java.lang.reflect.InvocationTargetException
08-07 17:14:31.682: E/AndroidRuntime(2201):     at java.lang.reflect.Method.invokeNative(Native Method)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at java.lang.reflect.Method.invoke(Method.java:511)
08 -07 17:14:31.682: E/AndroidRuntime(2201):    at android.view.View$1.onClick(View.java:3594)
08-07 17:14:31.682: E/AndroidRuntime(2201):     ... 11 more
08-07 17:14:31.682: E/AndroidRuntime(2201): Caused by: java.lang.NullPointerException
08-07 17:14:31.682: E/AndroidRuntime(2201):     at com.L00081183.mygpstool.MyGPSToolDirections.getDirection(MyGPSToolDirections.java:54)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at com.L00081183.mygpstool.MainActivity.onClick_GetDirections(MainActivity.java:145)

0条回答
登录 后发表回答