title: NullPointerException when drawing getting path in android
body: <p>I'm trying to ask Google for a path between two geographic points to draw. But I have nullpointerexception. </p>
  
  <p>Here, I call the method that makes the peticion:</p>
  
  <pre><code>    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();
      }
  </code></pre>
  
  <p>And this is the method:</p>
  
  <pre><code>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  
              + "&amp;destination=" + end.latitude + "," + end.longitude 
              + "&amp;sensor=false&amp;units=metric&amp;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;
  }
  </code></pre>
  
  <p>The stractrace of logcat:</p>
  
  <pre><code>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.&lt;init&gt;(HttpConnection.java:70)
  05-04 14:29:43.550: E/AndroidRuntime(11689):    at libcore.net.http.HttpConnection.&lt;init&gt;(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)
  </code></pre>