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