James Boyle
James Boyle

Reputation: 27

Location NullPointerException error on an Android-GeoFire project

Just getting used to Java and Android and I've got the above error.

It's triggered when I hit a button on the app that will look for the user type of "responder". The app crashes and I get this error:

 java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
                      at com.jamesboyle.firsthelp.PatientMapActivity$2.onClick(PatientMapActivity.java:83)

I'm not sure what is the object that is null right now from this

mRequest.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();

                    DatabaseReference ref = FirebaseDatabase.getInstance().getReference("patientRequest");
                    GeoFire geoFire = new GeoFire(ref);
                    geoFire.setLocation(userID, new GeoLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude()));

                    pickupLocation = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
                    mMap.addMarker(new MarkerOptions().position(pickupLocation).title("Respond Now"));

                    mRequest.setText("Locating Responder...");

                    getClosestResponder();
                }
            });
        }
        private int radius =1;
        private Boolean responderFound = false;
        private String responderFoundID;
        private void getClosestResponder(){
            DatabaseReference responderLocation = FirebaseDatabase.getInstance().getReference().child("respondersAvailable");

            GeoFire geoFire = new GeoFire(responderLocation);

            GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(pickupLocation.latitude, pickupLocation.longitude), radius);
            geoQuery.removeAllListeners();

            geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
                @Override
                public void onKeyEntered(String key, GeoLocation location) {
                    if (!responderFound) {
                        responderFound = true;
                        responderFoundID = key;
                    }
                }

Upvotes: 0

Views: 575

Answers (1)

user12268232
user12268232

Reputation: 11

Replace

pickupLocation = new LatLng(mLastLocation.getLatitude(),mLastLocation.getLongitude());` with `geoFire.setLocation("firebase-hq", new GeoLocation(37.7853889, -122.4056973));

For more info and understanding visit GeoFire for Java — Realtime location queries with Firebase

Upvotes: 1

Related Questions