title: Getting error java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()'
body: <p>Here is the error message from the log cat;</p>
  
  <p>I am getting the following error: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference</p>
  
  <p>Process: com.marktech.myrider, PID: 20905</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
      at com.marktech.myrider.CustomerMapActivity$2.onClick(CustomerMapActivity.java:94)
      at android.view.View.performClick(View.java:6291)
      at android.view.View$PerformClick.run(View.java:24931)
      at android.os.Handler.handleCallback(Handler.java:808)
      at android.os.Handler.dispatchMessage(Handler.java:101)
      at android.os.Looper.loop(Looper.java:166)
      at android.app.ActivityThread.main(ActivityThread.java:7425)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
  </code></pre>
  
  <p>here is my code</p>
  
  <p>The error is on this line; </p>
  
  <p>geoFire.setLocation( userId, new GeoLocation( mLastLocation.getLatitude(), mLastLocation.getLongitude() ), new GeoFire.CompletionListener() {</p>
  
  <pre><code>private GoogleMap mMap;
  GoogleApiClient mGoogleApiClient;
  Location mLastLocation;
  LocationRequest mLocationRequest;
  
  private Button btnLogout, btnRequest;
  private LatLng pickupLocation;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate( savedInstanceState );
      setContentView( R.layout.activity_customer_map );
      // Obtain the SupportMapFragment and get notified when the map is ready to be used.
      SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
              .findFragmentById( R.id.map );
      assert mapFragment != null;
      mapFragment.getMapAsync( this );
  
      btnLogout = (Button) findViewById( R.id.btnLogout );
      btnRequest = (Button) findViewById( R.id.btnRequest );
  
      btnLogout.setOnClickListener( new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              FirebaseAuth.getInstance().signOut();
              Intent intent = new Intent( CustomerMapActivity.this, MainActivity.class);
              startActivity( intent );
              finish();
              return;
  
          }
      } );
  
      btnRequest.setOnClickListener( new View.OnClickListener() {
          @Override
          public void onClick(View view) {
  
              if(mLastLocation!= null){
                  double latitude = mLastLocation.getLatitude();
                  double longitude = mLastLocation.getLongitude();
              }
              String userId = FirebaseAuth.getInstance().getUid();
  
              DatabaseReference ref = FirebaseDatabase.getInstance().getReference("customerRequest");
              GeoFire geoFire = new GeoFire( ref );
              geoFire.setLocation( userId, new GeoLocation( mLastLocation.getLatitude(), mLastLocation.getLongitude() ), new GeoFire.CompletionListener() {
                  @Override
                  public void onComplete(String key, DatabaseError error) {
  
  
              }
              });
              pickupLocation = new LatLng( mLastLocation.getLatitude(), mLastLocation.getLongitude() );
              mMap.addMarker( new MarkerOptions().position( pickupLocation ).title( "Pickup Here" ) );
              btnRequest.setText( "Getting your rider..." );
  
          }
      } );
  
  
  }
  
  
  @Override
  public void onMapReady(GoogleMap googleMap) {
      mMap = googleMap;
  
      if((ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)&amp;&amp;
              (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)&amp;&amp;
              (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED))
      {
          return;
      }
  
      buildGoogleApiClient();
  
      // mMap.setMyLocationEnabled( true );
  
  }
  
  private synchronized void buildGoogleApiClient(){
      mGoogleApiClient = new GoogleApiClient.Builder(this)
              .addConnectionCallbacks( this )
              .addOnConnectionFailedListener( this )
              .addApi( LocationServices.API )
              .build();
      mGoogleApiClient.connect();
  }
  
  @Override
  public void onLocationChanged(Location location) {
      mLastLocation = location;
  
      mMap.setMyLocationEnabled( true );
      mMap.getUiSettings().setMyLocationButtonEnabled(true);
  
      LatLng latlng = new LatLng( location.getLatitude(),location.getLongitude() );
      //moving the camera with the user
      mMap.moveCamera( CameraUpdateFactory.newLatLng( latlng  ) );
      mMap.animateCamera( CameraUpdateFactory.zoomTo( 11 ) );
  
  }
  
  @Override
  public void onStatusChanged(String s, int i, Bundle bundle) {
  
  }
  
  @Override
  public void onProviderEnabled(String s) {
  
  }
  
  @Override
  public void onProviderDisabled(String s) {
  
  }
  
  @Override
  public void onConnected(@Nullable Bundle bundle) {
      mLocationRequest = new LocationRequest();
      mLocationRequest.setInterval( 1000 );
      mLocationRequest.setFastestInterval( 1000 );
      mLocationRequest.setPriority( LocationRequest.PRIORITY_HIGH_ACCURACY );
  
      if((ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)&amp;&amp;
              (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)&amp;&amp;
              (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED))
      {
          return;
      }
  
      if (mGoogleApiClient != null) {
          LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
      }
  
      //LocationServices.FusedLocationApi.requestLocationUpdates( mGoogleApiClient, mLocationRequest, this );
  }
  
  @Override
  public void onConnectionSuspended(int i) {
  
  }
  
  @Override
  public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
  
  }
  
  @Override
  protected void onStop() {
      super.onStop();
  }
  </code></pre>
  
  <p>}</p>