Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9085133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:10:17+00:00 2026-06-16T21:10:17+00:00

I’ve been following this tutorial. I got a null pointer at doc = (Document)

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:10:18+00:00Added an answer on June 16, 2026 at 9:10 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.