title: Taking Current Location via Google Maps API
body: <p>What I want to do is putting a mark on the current location of the user. I get an error like <code>java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference</code>.
  Why <code>getLatitude</code> method returns null and what can I do to fix that? Thanks for your help.
  Here is my code:</p>
  <pre class="lang-java prettyprint-override"><code>public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
  
      public GoogleMap mMap;
      public Location location;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_maps);
          // Obtain the SupportMapFragment and get notified when the map is ready to be used.
          SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                  .findFragmentById(R.id.map);
          mapFragment.getMapAsync(this);
      }
  
      
      @Override
      public void onMapReady(GoogleMap googleMap) {
          mMap = googleMap;
          // Add a marker in Sydney and move the camera
  
          LatLng currentLocation = new LatLng(location.getLatitude(), location.getAltitude());
          mMap.addMarker(new MarkerOptions().position(currentLocation).title(&quot;You are here&quot;));
          mMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation));
      }
  }
  </code></pre>