0

I'm working on a simple app that can sign-in/sign-up.

Everything works normally except when I try to get data from the "Users" path on Realtime Database, I always get null for UID even though I already pushed my data and this is not a too complex part and I can get data normally with the exact same type others data?

This is the part where i define User object:

public class User {

    String profile_picture_url , username , user_email, user_uid;

    public User(){

    }

    public User(String profile_picture_url , String userUID , String username,String user_email)
    {
        this.profile_picture_url = profile_picture_url;
        this.user_uid = userUID;
        this.username = username;
        this.user_email = user_email;
    }

    //GETTER
    public String getProfile_picture_url() {
        return profile_picture_url;
    }

    public String getUsername() {
        return username;
    }

    public String getUser_EMail() {
        return user_email;
    }

    public String getUserUID() {
        return user_uid;
    }
}

This is my json tree on Firebase: enter image description here

This is the part where I retrieve the data:

private void readUserFromFB() {
    FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    Log.d("firebase_debug2",firebaseUser.getUid());
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
            userList.clear();
            for (DataSnapshot snapshot1 : snapshot.getChildren()) {
                User user = snapshot1.getValue(User.class);

                Log.d("firebase_debug2", " This is email " + user.getUser_EMail());
                Log.d("firebase_debug2","This is uid " + user.getUserUID());
                Log.d("firebase_debug2", "This is username " + user.getUsername());

              
                userAdapter = new UserAdapter(getContext(), userList);
                recyclerView.setAdapter(userAdapter);


            }
        }

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

        }
    });

}

Get this in logcat:

This is email tas@gmail.com
This is uid null
This is username tas
This is email wwqw@gmail.com
This is uid null
This is username wiiwi

Why do I get that annoying problem, hope somebody can help me find out because I tried for hours but can't figure out where I've missed.Thanks

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
phvtquang
  • 35
  • 5

1 Answers1

0

The property in the JSON of the database is called user_uid, but in your Java code you have:

public String getUserUID() {
    return user_uid;
}

Firebase follows JavaBean property naming conventions, so the getter for property user_uid should be:

public String getUser_uid() {
    return user_uid;
}

Similarly the setter should be setUser_uid(String newUid), or the field should be user_uid if that is public.

Alternatively you can change the property name in the JSON to userUID, so that it matches your code, or use annotations to explicitly tell Firebase the name of the property in the database:

@PropertyName("user_uid")
public String getUserUID() {
    return user_uid;
}

Also check out:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807