0

I'm trying to get the room and i get error with my existing query, how can i make it work after getting the reference for room, how can i call or get the id encircled in the picture? The id refers to Instrutor key


private void joinClass(cList ckey) {

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("StudentCourseList").child(currentUser.getUid()).push();
    DatabaseReference ingDBref = FirebaseDatabase.getInstance().getReference("Room");

    final String RoomCode=popupTitle.getText().toString();
    
    ingDBref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot data: dataSnapshot.getChildren()){
                if(data.child("courseKey").getValue().equals(RoomCode)){
                    myRef.setValue(ckey);
                    showMessage("Joined Successfully");
                    popupClickProgress.setVisibility(View.INVISIBLE);
                    popupAddBtn.setVisibility(View.VISIBLE);
                    popAddCourse.dismiss();
                }
                else{
                    showMessage("Please verify room code");
                    popupAddBtn.setVisibility(View.VISIBLE);
                    popupClickProgress.setVisibility(View.INVISIBLE);
                }
            }
        }
Kazz Domi
  • 17
  • 1
  • 5
  • Answer on how to parse the snapshot below, but if that doesn't solve the problem (and in general going forward) instead of saying "i get error", please include the exact error message and stack trace in your question. – Frank van Puffelen Nov 06 '22 at 19:48

1 Answers1

0

Since your ingDBref is loading all data under Room, the dataSnapshot in your onDataChange has all that data, including the two nested levels of dynamic keys. That means you also need two nested loops to get to the level of the individual courses:

ingDBref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot userSnapshot: dataSnapshot.getChildren()){
            Log.i("Firebase", userSnapshot.getKey()); // 
            for(DataSnapshot data: userSnapshot.getChildren()){ // 
                if(data.child("courseKey").getValue().equals(RoomCode)){ // 
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • i get this error.... Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference at com.educational.edukids.Student.Student_Dashboard$4.onDataChange(Student_Dashboard.java:165) – Kazz Domi Nov 06 '22 at 21:00
  • java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference at com.educational.edukids.Student.Student_Dashboard$4.onDataChange(Student_Dashboard.java:165) – Kazz Domi Nov 06 '22 at 21:01
  • 2
    It looks like the `data` snapshot doesn't have a `courseKey` child then. – Frank van Puffelen Nov 06 '22 at 21:02
  • I have a courseKey, on the image i posted – Kazz Domi Nov 06 '22 at 21:05
  • I accept your answer because its working and it shows in my Firebase but i dont know why i still get this error java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference at com.educational.edukids.Student.Student_Dashboard$4.onDataChange(Student_Dashboard.java:165) – Kazz Domi Nov 10 '22 at 15:49
  • 1
    Troubleshooting NullPointerExceptions is a well established task by now, so I recommend following this guidance: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Frank van Puffelen Nov 10 '22 at 16:36