1

I am trying to get values of child 'class_password' & 'class_uuid', but it always gives me a null value. Why is it so?

Firebase Realtime database

Here is the code :

public class StudentJoinClassDialog {
    Context context;
    Dialog dialog;
    TextInputEditText classCode, classPassword;
    Button studentJoinClass;
    String selectedClasscode, selectedClassPass;
    String checkPassword, checkClassUuid;
    String TAG = "StudentJoinClassDialog";

    public StudentJoinClassDialog(@NonNull Context mContext) {
        this.context = mContext;
        dialog = new Dialog(context);

    }

    public void showDialog() {
        dialog.setContentView(R.layout.student_join_class_dialog_layout);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.R.attr.colorBackground));
        dialog.create();
        dialog.show();

        // IDs //
        classCode = dialog.findViewById(R.id.enter_class_code_input_edit_text);
        classPassword = dialog.findViewById(R.id.enter_class_password_inputedittext);
        studentJoinClass = dialog.findViewById(R.id.join_class_btn);
        // IDs //**

        studentJoinClass.setOnClickListener(view -> {
            selectedClassPass = classPassword.getEditableText().toString().trim();

            Log.d(TAG, "class UUID : " + selectedClasscode + " class Password : " + selectedClassPass);
            Log.d(TAG, "classbook path : " + "classbook " + "/" + selectedClasscode + "/" + "class_information");

            FirebaseDatabase
                    .getInstance()
                    .getReference()
                    .child("classbook")
                    .child(classCode.getEditableText().toString().trim())
                    .child("class_information")
                    .addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot snapshot) {
                            checkClassUuid = snapshot.child("class_uuid").getValue(String.class);
                            checkPassword = snapshot.child("class_password").getValue(String.class);
                            if (selectedClasscode.equals(checkClassUuid) && selectedClassPass.equals(checkPassword)) {
                                Toast.makeText(context, "You are in", Toast.LENGTH_LONG).show();
                            } else {
                                Toast.makeText(context, "Class does not exist", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError error) {

                        }
                    });
            Log.d(TAG, "Retrieved Class UUID : " + checkClassUuid + " Retrieved Class Password : " + checkPassword);
        });
    }
}

Log :

2021-11-06 20:32:33.409 8071-8071/com.vrs.attendy D/StudentJoinClassDialog: class UUID : null class Password : 123 2021-11-06 20:32:33.409 8071-8071/com.vrs.attendy D/StudentJoinClassDialog: classbook path : classbook /null/class_information 2021-11-06 20:32:33.409 8071-8071/com.vrs.attendy D/StudentJoinClassDialog: Retrieved Class UUID : null Retrieved Class Password : null

Vinayak Sutar
  • 305
  • 2
  • 10
  • Data is retrieved from Firebase (and most modern cloud APIs) asynchronously. While this is going on your main code continues to execute. And then when the data is available, your callback is called. This means in you code that the `Log.d(TAG, "Retrieved Class UUID : " + checkClassUuid...` runs before `checkClassUuid = snapshot.child("class_uuid").getValue(String.class)` ever executes. If you run the code in a debugger and set breakpoints, you can most easily verify this. In practice it means that all code that needs the data, myst be inside the `onDataChange` or be called from there. – Frank van Puffelen Nov 06 '21 at 15:55
  • Thank you for your valuable information. @FrankvanPuffelen – Vinayak Sutar Nov 07 '21 at 07:21

0 Answers0