title: NullPointerException - Attempt to read from field latitude on a null object reference
body: <p>I have been working on a map based android app in Android Studio using Java for finding and I've been trying to create a polyline in between two markers on the map for a while but this NullPointerException keeps showing:</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to read from field 'double com.google.android.gms.maps.model.LatLng.latitude' on a null object reference
   at com.example.waynedeng.mapx.Home.MapsActivity.getRequestUrl(MapsActivity.java:267)
   at com.example.waynedeng.mapx.Home.MapsActivity.polylineSetup(MapsActivity.java:261)
   at com.example.waynedeng.mapx.Home.MapsActivity.onMapReady(MapsActivity.java:180)
   at com.google.android.gms.maps.zzaj.zza(Unknown Source:7)
   at com.google.android.gms.maps.internal.zzaq.onTransact(Unknown Source:18)
   at android.os.Binder.transact(Binder.java:627)
   at fg.b(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):19)
   at com.google.android.gms.maps.internal.bg.a(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):5)
   at com.google.maps.api.android.lib6.impl.be.run(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):5)
   at android.os.Handler.handleCallback(Handler.java:790)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:164)
   at android.app.ActivityThread.main(ActivityThread.java:6494)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
  </code></pre>
  
  <p>Here are my declarations:</p>
  
  <pre><code>private GoogleMap mMap;
  private Location lastKnownLocation;
  private FusedLocationProviderClient client;
  private ArrayList&lt;LatLng&gt; points;
  private Handler handler;
  private LatLng you = getLatLng(), destination = new LatLng(37.727812, -122.405864);
  private LocationManager locationManager;
  private List&lt;Address&gt; addresses;
  private Geocoder geocoder;
  private MarkerOptions youMarkerO, destinationMarkerO;
  private Marker youMarker, destinationMarker;
  </code></pre>
  
  <p>The error location seems to be in the getDirections() method:</p>
  
  <pre><code>private String getDirections(String reqUrl) throws IOException {
      String response = "";
      InputStream inputStream = null;
      HttpURLConnection con = null;
      try {
          URL url = new URL(reqUrl);
          con = (HttpURLConnection) url.openConnection();
          con.connect();
  
          inputStream = con.getInputStream();
          InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
          BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
  
          StringBuffer stringBuffer = new StringBuffer();
          String line = ""; //ERROR
          while((line = bufferedReader.readLine()) != null){
              stringBuffer.append(line);
          }
  
          response = stringBuffer.toString();
          inputStreamReader.close();
          bufferedReader.close();
      } catch (Exception e) {
          e.printStackTrace();
      }
      finally {
          if(inputStream !=null){
              inputStream.close();
          }
          con.disconnect();
      }
      return response;
  }
  </code></pre>
  
  <p>And in the overrided onLocationChanged() method:</p>
  
  <pre><code>locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
      locationListener = new LocationListener() {
  
          //Update location
          @Override
          public void onLocationChanged(Location location) {
  
              saveInfo();
              lastKnownLocation = location;
              Log.d(TAG, location.toString());
              Log.d(TAG, "Map cleared");
  
              mMap.clear();
  
              you = new LatLng(location.getLatitude(), location.getLongitude());
  
              mMap.getUiSettings().setZoomControlsEnabled(true);
  
              mMap.addMarker(youMarkerO);
  
              moveCamera(you, 0.5f);
  
          }
  
          @Override
          public void onStatusChanged(String provider, int status, Bundle extras) {
  
          }
  
          @Override
          public void onProviderEnabled(String provider) {
  
          }
  
          @Override
          public void onProviderDisabled(String provider) {
  
          }
  
      };
  </code></pre>
  
  <p>And here is my getRequestUrl() method:</p>
  
  <pre><code>private String getRequestUrl(LatLng one, LatLng two){
      if( one != null &amp;&amp; two != null){
          String origin = "origin="+ one.latitude + "," + one.longitude;
          String dest = "destination="+ two.latitude + "," + two.longitude;
          String sensor = "sensor=false";
          String mode = "mode=driving";
          String param = origin + "&amp;" + dest + "&amp;" + sensor + "&amp;" + mode;
          String output = "json";
          String baseUrl = "https://maps.googleapis.com/maps/api/directions/";
          String requestUrl = baseUrl + output + "?" + param;
          return requestUrl;
      }
      return null;
  }
  </code></pre>
  
  <p>At first, I thought it was because I accessed "you" and "destination" in the polylineSetup() before it was initialized but I changed that and it still doesn't work. I tried to solve it and read this question: <a href="https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it">What is a NullPointerException, and how do I fix it?</a> but it didn't cover the problem. Any info or fixes would help. Thanks :)</p>