Shailesh200
Shailesh200

Reputation: 49

How do I get/retrieve multiple data from uid in the firebase database in android?

How do I get the data from the firebase using following code?
I am getting a null pointer exception, after signed in successfully?

DatabaseReference mref = FirebaseDatabase.getInstance().getReference().child("/Users/Students")
                    .child(FirebaseAuth.getInstance().getCurrentUser().getUid());
            mref.addValueEventListener(new ValueEventListener() {
                @SuppressLint("SetTextI18n")
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for (DataSnapshot ds : dataSnapshot.getChildren()){

                        email = ds.child("email").getValue().toString();
                        full_name = ds.child("full_name").getValue().toString();
                        roll_number = ds.child("roll_number").getValue().toString();
                        admission_id = ds.child("admission_id").getValue().toString();
                        phone_no = ds.child("phone_number").getValue().toString();
                        branch = ds.child("branch").getValue().toString();
                        year = ds.child("year").getValue().toString();
                        semester = ds.child("semester").getValue().toString();
                        section = ds.child("section").getValue().toString();
                        course = ds.child("course").getValue().toString();
                        batch = ds.child("batch").getValue().toString();
                        dob = ds.child("dob").getValue().toString();
                        address = ds.child("address").getValue().toString();
                    }

                }

My Firebase Database is:

enter image description here

Upvotes: 2

Views: 163

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80944

Remove the following:

 for (DataSnapshot ds : dataSnapshot.getChildren()){

Since you already have access to the uid, then you do not need to loop inside the direct children to get the values..

So just do this:

 public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    email = dataSnapshot.child("email").getValue().toString();
                    full_name = dataSnapshot.child("full_name").getValue().toString();
                    roll_number = dataSnapshot.child("roll_number").getValue().toString();
                    admission_id = dataSnapshot.child("admission_id").getValue().toString();
                    phone_no = dataSnapshot.child("phone_number").getValue().toString();
                    branch = dataSnapshot.child("branch").getValue().toString();
                    year = dataSnapshot.child("year").getValue().toString();
                    semester = dataSnapshot.child("semester").getValue().toString();
                    section = dataSnapshot.child("section").getValue().toString();
                    course = dataSnapshot.child("course").getValue().toString();
                    batch = dataSnapshot.child("batch").getValue().toString();
                    dob = dataSnapshot.child("dob").getValue().toString();
                    address = dataSnapshot.child("address").getValue().toString();

Upvotes: 3

Related Questions