Reputation:
I am attempting to pull data from firebase and display the user's name and status. I am currently encountering a null point exception.
public class ProfileFragment extends Fragment {
private DatabaseReference mUserDatabase;
private FirebaseUser mCurrentUser;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
CircleImageView displayProfilePicture = view.findViewById(R.id.settings_profile_picture);
final TextView mName = view.findViewById(R.id.settings_user_name);
final TextView mStatus = view.findViewById(R.id.settings_user_status);
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
String current_uid = Objects.requireNonNull(mCurrentUser).getUid();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String name = (Objects.requireNonNull(dataSnapshot.child("name").getValue())).toString();
String status = Objects.requireNonNull(dataSnapshot.child("status").getValue()).toString();
String image = Objects.requireNonNull(dataSnapshot.child("image").getValue()).toString();
String thumb_image = Objects.requireNonNull(dataSnapshot.child("thumb image ").getValue()).toString();
mName.setText(name);
mStatus.setText(status);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return view;
}}
I have also tried to implement this in a different way using this code but i am still failing to display the data.
public class ProfileFragment extends Fragment {
private DatabaseReference mUserDatabase;
private FirebaseUser mCurrentUser;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
CircleImageView displayProfilePicture = view.findViewById(R.id.settings_profile_picture);
final TextView mName = view.findViewById(R.id.settings_user_name);
final TextView mStatus = view.findViewById(R.id.settings_user_status);
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
String current_uid = Objects.requireNonNull(mCurrentUser).getUid();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String keys=datas.getKey();
String displayname=datas.child("name").getValue().toString();
mName.setText(displayname);
}}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return view;
}}
and my database is structured like this:
Upvotes: 0
Views: 72
Reputation: 80924
Since you are already have the userId
then you do not need to loop to be able to access the direct children.
Try the following:
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String keys=dataSnapshot.getKey();
String displayname=dataSnapshot.child("name").getValue().toString();
mName.setText(displayname);
}}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Upvotes: 1