1

After the user has made an appointment, the attribute(user id, date, time) is stored in the appointment table with the therapist push id as the parent node(to facilitate the retrieval of certain doctor's appointment).

Now, in the user interface, I need to get all the attribute by using the user id. Does anyone know how to do it?

Here is my database structure

database struc

Here is my own code

a=new ArrayList<AppointmentObject>();

databaseReference= FirebaseDatabase.getInstance().getReference();
databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        //to get current log in user id
        String userid1 = FirebaseAuth.getInstance().getCurrentUser().getUid();

        for(DataSnapshot dataSnapshot1: dataSnapshot.child("appointment").getChildren()) {

            //if the userid stored is same as user id, it will be added to arraylist
            if(dataSnapshot1.getValue(AppointmentObject.class).getUserid().equals(userid1)){
                AppointmentObject thera= dataSnapshot1.getValue(AppointmentObject.class);
                a.add(thera);
            }
        }

        adapter=new MyRecyclerviewPAppointment(MainActivityPAppointment.this, a);
        rv.setAdapter(adapter);
    }

However, it appears this error

error

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
h.s
  • 33
  • 7
  • For future, please include all textual content as text (no screenshots) as that makes them searchable en reusable. For the database JSON, you can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Jul 26 '19 at 12:59

1 Answers1

1

Your NullPointerException comes from this line

if(dataSnapshot1.getValue(AppointmentObject.class).getUserid().equals(userid1)) {}

One of these fields is null dataSnapshot1, dataSnapshot1.getValue(), dataSnapshot1.getValue().getUserid()

To ensure your code will compile reverse order of comparison:

if(userid1.equals(dataSnapshot1.getValue(AppointmentObject.class).getUserid())) {}
IQbrod
  • 2,060
  • 1
  • 6
  • 28