I am trying to update a user profile from Firestore in a Fragment, but each time I run the App I get this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference.
Please your help will be much appreciated, am still a newbie am sorry if my question might offend
public class ProfileFragment extends Fragment {
TextView personName, mAge, bloodGroup, mGender, mHeight, mWeight;
ImageView settingsButton;
String userID;
FirebaseAuth firebaseAuth;
FirebaseFirestore firebaseFirestore;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.profile_fragment, container, false);
personName = view.findViewById(R.id.text_person_name);
bloodGroup = view.findViewById(R.id.blood_group);
mAge = view.findViewById(R.id.profile_age);
mGender = view.findViewById(R.id.gender);
mHeight = view.findViewById(R.id.profile_height);
mWeight = view.findViewById(R.id.profile_weight);
settingsButton = view.findViewById(R.id.profile_settings);
firebaseAuth = FirebaseAuth.getInstance();
firebaseFirestore = FirebaseFirestore.getInstance();
// Retrieve UserID
userID = firebaseAuth.getCurrentUser().getUid();
DocumentReference documentReference = firebaseFirestore.collection("patients").document(userID);
documentReference.addSnapshotListener(getActivity(), new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@javax.annotation.Nullable DocumentSnapshot documentSnapshot, @javax.annotation.Nullable FirebaseFirestoreException e) {
personName.setText(documentSnapshot.getString("Name"));
bloodGroup.setText(documentSnapshot.getString("BloodGroup"));
mAge.setText(documentSnapshot.getString("Age"));
mGender.setText(documentSnapshot.getString("Gender"));
mHeight.setText(documentSnapshot.getString("Height"));
mWeight.setText(documentSnapshot.getString("Weight"));
}
});
return view;
}
}