title: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference[dup]
body: <p>I tried to get distance between two points </p>
  
  <p>This is the error </p>
  
  <blockquote>
    <p>java.lang.NullPointerException: Attempt to invoke virtual method 'int
    java.lang.String.length()' on a null object reference
                          at com.example.android.algorithm.MainActivity.distancematrix(MainActivity.java:209)
                          at com.example.android.algorithm.MainActivity$1.onClick(MainActivity.java:92)
                          at android.view.View.performClick(View.java:5207)
                          at android.view.View$PerformClick.run(View.java:21177)
                          at android.os.Handler.handleCallback(Handler.java:739)
                          at android.os.Handler.dispatchMessage(Handler.java:95)
                          at android.os.Looper.loop(Looper.java:148)
                          at android.app.ActivityThread.main(ActivityThread.java:5441)
                          at java.lang.reflect.Method.invoke(Native Method)
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)</p>
  </blockquote>
  
  <p>This is the code portion that Cause the error</p>
  
  <pre><code>public  void distancematrix()
       {
           String k="str";
           double lat1,lat2,lng1,lng2;
           for(int i=0;i&lt;5;i++) {
               lat1=givenlatlng[i][0];
               lng1=givenlatlng[i][1];
  
               for (int j = 0; j &lt; 5; j++) {
                   lat2=givenlatlng[j][0];
                   lng2=givenlatlng[j][1];
                   k=getDistance(lat1,lng1,lat2,lng2);
                   DistanceMatrix[i][j] = Double.parseDouble(k.substring(0,k.length()-2));
               }
  
           }
  
       }
  </code></pre>
  
  <p>The givenlatlang contains latitude &amp; longitude</p>
  
  <p>the getDistance function</p>
  
  <pre><code>public String getDistance(final double lat1, final double lon1, final double lat2, final double lon2){
          final String[] parsedDistance = new String[1];
          final String[] response = new String[1];
          Thread thread=new Thread(new Runnable() {
              @Override
              public void run() {
                  try {
  
                      URL url = new URL("http://maps.googleapis.com/maps/api/directions/json?origin=" + lat1 + "," + lon1 + "&amp;destination=" + lat2 + "," + lon2 + "&amp;sensor=false&amp;units=metric&amp;mode=driving");
                      final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                      conn.setRequestMethod("POST");
                      InputStream in = new BufferedInputStream(conn.getInputStream());
                      response[0] = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
  
                      JSONObject jsonObject = new JSONObject(response[0]);
                      JSONArray array = jsonObject.getJSONArray("routes");
                      JSONObject routes = array.getJSONObject(0);
                      JSONArray legs = routes.getJSONArray("legs");
                      JSONObject steps = legs.getJSONObject(0);
                      JSONObject distance = steps.getJSONObject("distance");
                      parsedDistance[0] =distance.getString("text");
  
                  } catch (ProtocolException e) {
                      e.printStackTrace();
                  } catch (MalformedURLException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
              }
          });
          thread.start();
          try {
              thread.join();
          } catch (InterruptedException e) {``
              e.printStackTrace();
          }
          return parsedDistance[0];
      }
  }`
  </code></pre>