I was working on this User information project. I have created login registration for users. The project supposed to show information about logged-in users. When users log in to their profile, it is returning a null reference.
user = FirebaseAuth.getInstance().getCurrentUser(); reference = FirebaseDatabase.getInstance().getReference("Users"); userID = user.getUid(); final TextView emailTextView = (TextView) findViewById(R.id.emailAddress); final TextView fullNameTextView = (TextView) findViewById(R.id.fullName); final TextView phoneNumberTextView = (TextView) findViewById(R.id.phoneNumber); final TextView carNameTextView = (TextView) findViewById(R.id.carName); final TextView whereTextView = (TextView) findViewById(R.id.where); final TextView availableTextView = (TextView) findViewById(R.id.available); final ImageView imageViewImageView = (ImageView) findViewById(R.id.imageView); reference.child(userID).addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot snapshot) { User userProfile = snapshot.getValue(User.class); if (userProfile != null) { String fullName = userProfile.getFullName(); String phoneNumber = userProfile.phoneNumber; String carName = userProfile.carName; String email = userProfile.email; Boolean where = userProfile.where; Boolean isAvailable = userProfile.isAvailable; String imageView = userProfile.uriImage; fullNameTextView.setText(fullName); phoneNumberTextView.setText(phoneNumber); carNameTextView.setText(carName); emailTextView.setText(email); if (where == false) { whereTextView.setText("Tashkent"); } else { whereTextView.setText("Urgench"); } if (isAvailable == false) { availableTextView.setText("No"); } else { availableTextView.setText("Yes"); } Uri imgUri = Uri.parse(imageView); imageViewImageView.setImageURI(imgUri); } else { Toast.makeText(ProfileActivity.this, "poshol naxuy", Toast.LENGTH_SHORT).show(); } } @Override public void onCancelled(@NonNull DatabaseError error) { Toast.makeText(ProfileActivity.this, "Something wrong happened", Toast.LENGTH_SHORT).show(); } });
Advertisement
Answer
The key of the user node that is showing in your JSON (-MRF...J0KL
) was generated by calling push()
in a DatabaseReference
. It does not match a Firebase Authentication UID, so your reference.child(userID)
ends up pointing to a non-existing node. That means that the snapshot
is going to be empty, and snapshot.getValue(User.class)
correctly returns null
as no User
exists in the snapshot.
My guess is that you add this data with something like:
reference.push().setValue(userInfo);
Instead you should be writing it with:
reference.child(userID).setValue(userInfo);