I was following this tutorial on YouTube and i was doing the same, maybe i have misplaced or forgot a code somewhere which i can't yet recognize, i'm still new to this,
So the error i'm getting is.
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.www.riderapp.model.Rider.getName()' on a null object reference
at com.example.www.riderapp.Home$5$1.onDataChange(Home.java:391)
This is my Home.java file method
private void loadAllAvailableDrivers() {
//Load all drivers within 3km distance
DatabaseReference driverLocation = FirebaseDatabase.getInstance().getReference(Common.driver_tbl);
GeoFire gf = new GeoFire(driverLocation);
GeoQuery geoQuery = gf.queryAtLocation(new GeoLocation(mLastLocation.getLatitude(),mLastLocation.getLongitude()),distance);
geoQuery.removeAllListeners();
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, final GeoLocation location) {
// Use key to get email from table users
//Table Users is table when driver register account and update information
FirebaseDatabase.getInstance().getReference(Common.user_driver_tbl)
.child(key)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//Because Rider and User model is same properties
Rider rider = dataSnapshot.getValue(Rider.class);
//Add drivers to map
mMap.addMarker(new MarkerOptions()
.position(new LatLng(location.latitude, location.longitude))
.flat(true)
ERRR -----> .title(rider.getName())
.snippet("Phone : "+rider.getPhone())
.icon(BitmapDescriptorFactory.fromResource(R.drawable.car)));
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public void onKeyExited(String key) {
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
if(distance <= LIMIT) // distance just find for 3km
{
distance++;
loadAllAvailableDrivers();
}
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
});
}
And here is my Rider java class
public class Rider {
private String name,email,phone,password;
public Rider(){
}
public Rider(String name, String email, String phone, String password) {
this.name = name;
this.email = email;
this.phone = phone;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
I couldn't find why this is really happening, i think it should be a small thing ;)