0

I am having some problem with null pointer exception error message. Basically I have one method which returning Document object and my other methods will use the Document object as parameter.

Here is the reference which I get online:

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="+mode;

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

When I testing the URL, it did returning something. However, when it execute this method, it just keep returning me null for the Document object. Can somebody explain to me what does the Http part of code for? I am quite confused.

Thanks in advance.

Edited portion

LatLng fromPosition = new LatLng(1.3564, 103.8311);
            LatLng toPosition = new LatLng(1.3426, 103.9254);

            map = ((SupportMapFragment)getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
            GMapV2Direction md = new GMapV2Direction();

            Document doc = md.getDocument(fromPosition, toPosition, GMapV2Direction.MODE_DRIVING);
            System.out.println(doc);
            map.addMarker(new MarkerOptions().position(fromPosition).title("Start"));
            map.addMarker(new MarkerOptions().position(toPosition).title("End"));
            ArrayList<LatLng> directionPoint = md.getDirection(doc);
            PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);

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

            map.addPolyline(rectLine);
  • NullPointerException at com.example.googlemap.GMapV2Direction.getDirection(GMapV2Direction.java:111) check it once.. – Shailendra Madda Jul 26 '14 at 13:23
  • When I run in debug mode, the doc is null. Is it because of the getDirection() returning null? –  Jul 26 '14 at 13:31
  • can you also put the code where you are calling this method? – bitkot Jul 26 '14 at 13:55
  • @AmitChotaliya Sure, check my edited portion. Thanks! When I print out the doc from Edited portion, it is already returning me null value –  Jul 26 '14 at 14:00
  • @AmitChotaliya Any ideas or guides? –  Jul 26 '14 at 14:18
  • I tried the URL directly, so I don't think anything's wrong with that. Only part left is the try-catch block. – bitkot Jul 26 '14 at 14:20
  • there is not exception in that block? – bitkot Jul 26 '14 at 14:20
  • @AmitChotaliya I get the reference from here: http://stackoverflow.com/questions/14495030/get-driving-directions-using-google-maps-api-v2. Do you have any idea why is it so? –  Jul 26 '14 at 14:22

0 Answers0