0

I am trying to get the user UID from the Firebase auth, but it's giving me a null value. I have one admin which will register many schools and each school will have its own id by push key. The admin will regsiter schools as he wants and they can only login after they register them.

this is the firebase realtime

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        MngEmail = (EditText)findViewById(R.id.MngEmailField);
        MngPassword = (EditText)findViewById(R.id.mngPass);
        MngFrgtPass = (TextView)findViewById(R.id.MngForText);
        MngLoginBtn = (Button)findViewById(R.id.MngBtn);
        loadingbar = new ProgressDialog(this);

        MngAuth = FirebaseAuth.getInstance();
        SuperAdminUId = MngAuth.getCurrentUser().getUid();
        String key = MngRef.child("SchoolData").child(SuperAdminUId).getKey();

        MngRef = FirebaseDatabase.getInstance().getReference().child("SchoolData").child(SuperAdminUId).child(key);

        MngFrgtPass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                TO Dooo
            }
        });

        MngLoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Query query = MngRef.orderByChild("SchoolEmail").equalTo(MngEmail.getText().toString().trim());
                        query.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                if (dataSnapshot.exists()) {
                                    // dataSnapshot is the "issue" node with all children with id 0

                                    for (DataSnapshot user : dataSnapshot.getChildren()) {
                                        // do something with the individual "issues"
                                        School schoolinfo = user.getValue(School.class);

                                        if (schoolinfo.equals(MngPassword.getText().toString().trim())) {
                                            sendMngToMain();
                                        } else {
                                            Toast.makeText(login.this, "Password is wrong", Toast.LENGTH_LONG).show();
                                        }
                                    }
                                } else {
                                    Toast.makeText(login.this, "User not found", Toast.LENGTH_LONG).show();
                                }
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });
            }
        });
    }
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

1 Answers1

0

To solve this, please change the order of the following lines of code from:

String key = MngRef.child("SchoolData").child(SuperAdminUId).getKey();
MngRef = FirebaseDatabase.getInstance().getReference().child("SchoolData").child(SuperAdminUId).child(key);

to

MngRef = FirebaseDatabase.getInstance().getReference().child("SchoolData").child(SuperAdminUId).child(key);
String key = MngRef.child("SchoolData").child(SuperAdminUId).getKey();

So you are getting that exception because you are trying to use an object that wasn't yet initialized.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • ِand then I will get an error of the key string because I initialize it after calling it up. no thats not the issue – Ahmed A. Alqumairi Jan 23 '19 at 12:32
  • I am getting the exception while retrieving the admin id not the schools id – Ahmed A. Alqumairi Jan 23 '19 at 12:38
  • Please show me the error that you get after you switch those two lines and indicate at which exact line of code it occurs. – Alex Mamo Jan 23 '19 at 12:39
  • I am initializing the string key in the first line where I call the key in the second line at the end and I do that in order to get the push key. – Ahmed A. Alqumairi Jan 23 '19 at 12:43
  • This is not how you should get the pushed key. Your code will not work that way. The problem in your code was indeed the order of initialization. Please see my answer from this **[post](https://stackoverflow.com/questions/51787784/how-to-get-specific-pushedid-in-firebase/51788244)**, to see how you can get the pushed key using `push().getKey()` and tell me if it works. – Alex Mamo Jan 23 '19 at 12:49