Cannot Access Value of Cloud Firestore Child

I have a Firebase Cloud Firestore data structure as seen below and want to access "A1":

enter image description here

I am trying to access each "answer" document, which contains the actual answer and the subsequent vote count. I am then using this answer information to populate radio buttons.

Currently, I have a DocumentSnapshot reference at x9ojsg4tzLgx2SRTnchp. From that reference, I am calling get on the answers collection, as see below:

public void addRadioButtonsWithFirebaseAnswers(int numberOfAnswers, DocumentSnapshot documentSnapshot) {
    mPollAnswerArrayList = new ArrayList<RadioButton>();
    for (int i = (numberOfAnswers - 1); i >= 0; i--) {
        Log.e("Number of Answers", "The number of answers is " + numberOfAnswers);
        final int indexCreated = ((numberOfAnswers - 1) - i);
        mPollAnswerArrayList.add((indexCreated), new RadioButton((getActivity().getApplicationContext())));
        String firebaseChild = String.valueOf(indexCreated + 1);
        documentSnapshot.getReference().collection(ANSWERS_LABEL).document(firebaseChild).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                Answer answer = documentSnapshot.toObject(Answer.class);
                Log.v("TAG FOR DOCREF", answer.getAnswer());

And here is my Answer model:

    public class Answer {

      private String answer;
      private int vote_count;

      public Answer(String answer, int vote_count){
          this.answer = answer;
          this.vote_count = vote_count;

      public String getAnswer() {
          return answer;

      public int getVote_count() {
          return vote_count;

I am receiving a null pointer:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[])' on a null object reference at$zza.zza(Unknown Source) at Source) at Source) at Source) at com.troychuinard.fanpolls.Fragment.PollFragment$6.onSuccess( at com.troychuinard.fanpolls.Fragment.PollFragment$6.onSuccess( at Source) at android.os.Handler.handleCallback( at android.os.Handler.dispatchMessage( at android.os.Looper.loop( at at java.lang.reflect.Method.invoke(Native Method) at$ at

1 answer

  • answered 2018-02-13 02:49 Doug Stevenson

    Add a public no-arg constructor to Answer. All JavaBean type objects should have one so that reflection can be used to create an instance of it without having to guess how to pass parameters to it correctly.

    public Answer() {}