Helia
Helia

Reputation: 67

NullPointerException when drawing getting path in android

I'm trying to ask Google for a path between two geographic points to draw. But I have nullpointerexception.

Here, I call the method that makes the peticion:

    LatLng start = new LatLng(13.6871, 100.5352);
    LatLng end = new LatLng(13.6836, 100.5390);

    StringBuilder SBdoc = null;
    try {
        SBdoc = getDocument(start, end, MODE_DRIVING);
        if (SBdoc != null){
            String doc = SBdoc.toString();
        }else {Log.e("The route of the line is null","");}
    } catch (IOException e) {
        e.printStackTrace();
    }

And this is the method:

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

    StringBuilder builder = null;

    URL categoriesWSUrl = new URL(url);
    URLConnection connection = categoriesWSUrl.openConnection();

    String line;
    builder = new StringBuilder();

    InputStream response = connection.getInputStream();
    InputStreamReader isr = new InputStreamReader( response );
    BufferedReader reader = new BufferedReader(isr);
    while((line = reader.readLine()) != null){
        builder.append(line + "\n");
    }
    return builder;
}

The stractrace of logcat:

05-04 14:29:43.550: E/AndroidRuntime(11689): FATAL EXCEPTION: main
05-04 14:29:43.550: E/AndroidRuntime(11689): java.lang.RuntimeException: Unable to start activity ComponentInfo{package/package.Map}: android.os.NetworkOnMainThreadException
05-04 14:29:43.550: E/AndroidRuntime(11689):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at android.os.Looper.loop(Looper.java:137)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at android.app.ActivityThread.main(ActivityThread.java:5041)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at java.lang.reflect.Method.invokeNative(Native Method)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at java.lang.reflect.Method.invoke(Method.java:511)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at dalvik.system.NativeStart.main(Native Method)
05-04 14:29:43.550: E/AndroidRuntime(11689): Caused by: android.os.NetworkOnMainThreadException
05-04 14:29:43.550: E/AndroidRuntime(11689):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at package.Map.getDocument(Map.java:110)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at package.Map.onCreate(Map.java:84)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at android.app.Activity.performCreate(Activity.java:5104)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-04 14:29:43.550: E/AndroidRuntime(11689):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

Upvotes: 1

Views: 416

Answers (3)

Raghunandan
Raghunandan

Reputation: 133560

You should use a asynctask. If you run network related operations on your main UI thread you will get NetworkOnMainThreadException. This for HoneyComb and later versions. SO use asynctask

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

For asynctask check the link below

http://developer.android.com/reference/android/os/AsyncTask.html

Call your getDocument(params) in doInBackground()

Note: Do not update ui from doInbackground()

Do no forget to go through the topic under the heading The 4 steps.

Upvotes: 0

Guilherme Gregores
Guilherme Gregores

Reputation: 1048

You also should use AsyncTask for that method "getDocument", see AsyncTask

Example:AsyncTask Android example

Upvotes: 0

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

NetworkOnMainThreadException this error because you have to run some network related operations in background thread. use asynctask and write your code in doInBackground().

Upvotes: 1

Related Questions