-2

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 ;)

1 Answers1

0

Like i thought, the issue is your Rider model class which I couldn't see such related array or data in the Firebase json output. But i saw : DriversInformation

First, go to Firebase console from which your data(s) are available and copy-paste the output json to this website:

http://www.jsonschema2pojo.org/

Check source as json and make the output to GSON then after downloading the class, drag it to your project.

After that, you'll be able to get access to it like this:

ResultPojo result = dataSnapshot.getValue(ResultPojo.class);

Then, it will be fine to use or to get the data from the converted json to Gson. However, it could be done by looping through dataSnapshot:

ArrayList<String> names= new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {

             // blop blop ... do some magic with ds
   }
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108