-2

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;

    }
}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
JAtoms
  • 522
  • 5
  • 10
  • 1
    Is `personName`, `bloodGroup`, `mAge`, `mGender`, `mHeight` or `mWeight` null? – dan1st Dec 30 '19 at 21:47
  • 1
    Q: Exactly which line (i.e. which variable is "null")? SUGGESTION: set a breakpoint in `onCreateView()`, step through the code in the debugger (Android Studio?), and examine each TextView variable (personName, bloodGroup, etc) until you identify which one isn't being initialized. – FoggyDay Dec 30 '19 at 21:50

1 Answers1

-2

One of your TextViews is null. That means your inflated XML doesn't contain the ID of the view you're looking for. Without more details, it's impossible to tell for sure, but you should double check that the view IDs in your code exactly match those from the inflated XML.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thanks, the reference here personName = view.findViewById(R.id.text_person_name); is incorrect, ive made my corrections and code compliles – JAtoms Dec 30 '19 at 21:59
  • 1
    Downvote for not ensuring the OP pays close attention to line#s in the stack trace, and makes good use of the debugger – FoggyDay Dec 30 '19 at 22:09
  • 1
    @FoggyDay Thanks for the feedback. For the future, I'll suggest that *kind, helpful feedback* is more effective and appreciated rather than *criticizing feedback* accompanied by a downvote. Harsh feedback doesn't help others to feel like they want to participate in this community effort. It would also be welcome to simply propose an edit to improve the answer. – Doug Stevenson Dec 30 '19 at 23:41