I’ve been following this tutorial.
I got a null pointer at doc = (Document) urlConnection.getInputStream();.
Here is my code:
public class DirectionActivity extends MapActivity {
MapView myMapView = null;
MapController myMC = null;
GeoPoint geoPoint = null;
DocumentBuilderFactory dbf;
DocumentBuilder db;
Document doc;
HttpURLConnection urlConnection ;
URL url ;
String pathConent ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myMapView = (MapView) findViewById(R.id.mapview1);
geoPoint = null;
myMapView.setSatellite(false);
String pairs[] = getDirectionData("Ahmedabad", "Goa");
String[] lngLat = pairs[0].split(",");
// STARTING POINT
GeoPoint startGP = new GeoPoint(
(int) (Double.parseDouble(lngLat[1]) * 1E6), (int) (Double
.parseDouble(lngLat[0]) * 1E6));
myMC = myMapView.getController();
geoPoint = startGP;
myMC.setCenter(geoPoint);
myMC.setZoom(15);
myMapView.getOverlays().add(new DirectionPathOverlay(startGP, startGP));
// NAVIGATE THE PATH
GeoPoint gp1;
GeoPoint gp2 = startGP;
for (int i = 1; i < pairs.length; i++) {
lngLat = pairs[i].split(",");
gp1 = gp2;
// watch out! For GeoPoint, first:latitude, second:longitude
gp2 = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6),
(int) (Double.parseDouble(lngLat[0]) * 1E6));
myMapView.getOverlays().add(new DirectionPathOverlay(gp1, gp2));
Log.d("xxx", "pair:" + pairs[i]);
}
// END POINT
myMapView.getOverlays().add(new DirectionPathOverlay(gp2, gp2));
myMapView.getController().animateTo(startGP);
myMapView.setBuiltInZoomControls(true);
myMapView.displayZoomControls(true);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private String[] getDirectionData(String srcPlace, String destPlace) {
String urlString = "http://maps.google.com/maps?f=d&hl=en&saddr="
+ srcPlace + "&daddr=" + destPlace
+ "&ie=UTF8&0&om=0&output=kml";
Log.d("URL", urlString);
try {
url = new URL(urlString.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
dbf = DocumentBuilderFactory.newInstance();
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
***doc = (Document) urlConnection.getInputStream();***(getting doc=null)
} catch (Exception e) {
}
NodeList nl = doc.getElementsByTagName("LineString");
for (int s = 0; s < nl.getLength(); s++) {
Node rootNode = nl.item(s);
NodeList configItems = rootNode.getChildNodes();
for (int x = 0; x < configItems.getLength(); x++) {
Node lineStringNode = configItems.item(x);
NodeList path = lineStringNode.getChildNodes();
pathConent = path.item(0).getNodeValue();
}
}
String[] tempContent = pathConent.split(" ");
return tempContent;
}
public class DirectionPathOverlay extends Overlay {
private GeoPoint gp1;
private GeoPoint gp2;
public DirectionPathOverlay(GeoPoint gp1, GeoPoint gp2) {
this.gp1 = gp1;
this.gp2 = gp2;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
// TODO Auto-generated method stub
Projection projection = mapView.getProjection();
if (shadow == false) {
Paint paint = new Paint();
paint.setAntiAlias(true);
Point point = new Point();
projection.toPixels(gp1, point);
paint.setColor(Color.BLUE);
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setStrokeWidth(2);
canvas.drawLine((float) point.x, (float) point.y, (float) point2.x,
(float) point2.y, paint);
}
return super.draw(canvas, mapView, shadow, when);
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);
}
}
}
Here is Logcat:
01-03 10:56:33.447: E/AndroidRuntime(1018): FATAL EXCEPTION: main
01-03 10:56:33.447: E/AndroidRuntime(1018): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.itsmyway/com.example.itsmyway.DirectionActivity}: java.lang.NullPointerException
01-03 10:56:33.447: E/AndroidRuntime(1018): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-03 10:56:33.447: E/AndroidRuntime(1018): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-03 10:56:33.447: E/AndroidRuntime(1018): at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-03 10:56:33.447: E/AndroidRuntime(1018): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-03 10:56:33.447: E/AndroidRuntime(1018): at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 10:56:33.447: E/AndroidRuntime(1018): at android.os.Looper.loop(Looper.java:137)
01-03 10:56:33.447: E/AndroidRuntime(1018): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-03 10:56:33.447: E/AndroidRuntime(1018): at java.lang.reflect.Method.invokeNative(Native Method)
01-03 10:56:33.447: E/AndroidRuntime(1018): at java.lang.reflect.Method.invoke(Method.java:511)
01-03 10:56:33.447: E/AndroidRuntime(1018): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-03 10:56:33.447: E/AndroidRuntime(1018): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-03 10:56:33.447: E/AndroidRuntime(1018): at dalvik.system.NativeStart.main(Native Method)
01-03 10:56:33.447: E/AndroidRuntime(1018): Caused by: java.lang.NullPointerException
01-03 10:56:33.447: E/AndroidRuntime(1018): at com.example.itsmyway.DirectionActivity.getDirectionData(DirectionActivity.java:109)
01-03 10:56:33.447: E/AndroidRuntime(1018): at com.example.itsmyway.DirectionActivity.onCreate(DirectionActivity.java:41)
01-03 10:56:33.447: E/AndroidRuntime(1018): at android.app.Activity.performCreate(Activity.java:5008)
01-03 10:56:33.447: E/AndroidRuntime(1018): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-03 10:56:33.447: E/AndroidRuntime(1018): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-03 10:56:33.447: E/AndroidRuntime(1018): ... 11 more
I had implemented the same example and was facing the same problem.
What your application does is to retrieve a document from google servers and then look in that document for the directions. Although the kml document (the one that your code wants to retrieve) used to work a few months (like when that tutorial was written,)now it does not work since the google updated their API in December 2012.
Solution : your URL appears to be deprecated. Instead of kml, Google maps uses JSON and XML.
Look at this page for using web services.
Now,
1) Create a URL AS PER THE GUIDE on these web services.
2) Now send a HTTP Connection request to the google servers and you must get an Inputstream.
Your question is over, but if you are using the same code after solving this problem you will get another problem of parsing the received document.
Solution to the second problem :
1) Save the document in the sdcard of cellphone and view it manually. A guide to store the document is here.
2) find the directions in the document according to the XML output section on this page.
3) Retrieve those directions using a DocumentBuilderFactory or otherwise.
your application looks for the tag “LineString” in the kml document. However, since now you are using XML or JSON, look for the relevant tags as given in the guide.