0

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;
                    }
                }
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – ADM May 10 '18 at 14:12
  • 1
    Looks like `mLastLocation` is `null` in the statement `geoFire.setLocation(userID, new GeoLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude())); .` – ADM May 10 '18 at 14:13

1 Answers1

1

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

il_raffa
  • 5,090
  • 129
  • 31
  • 36
  • From Review: Hi, while links are great way of sharing knowledge, they won't really answer the question if they get broken in the future. Add to your answer the essential content of the link which answers the question. In case the content is too complex or too big to fit here, describe the general idea of the proposed solution. Remember to always keep a link reference to the original solution's website. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – sɐunıɔןɐqɐp Oct 24 '19 at 10:36