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())));
        mPollAnswerArrayList.get(indexCreated).setId(indexCreated);
        String firebaseChild = String.valueOf(indexCreated + 1);
        documentSnapshot.getReference().collection(ANSWERS_LABEL).document(firebaseChild).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                Answer answer = documentSnapshot.toObject(Answer.class);
                mPollAnswerArrayList.get(indexCreated).setText(answer.getAnswer());
                Log.v("TAG FOR DOCREF", answer.getAnswer());
            }
        });

And here is my Answer model:

    @IgnoreExtraProperties
    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 com.google.android.gms.internal.zzevb$zza.zza(Unknown Source) at com.google.android.gms.internal.zzevb.zza(Unknown Source) at com.google.android.gms.internal.zzevb.zza(Unknown Source) at com.google.firebase.firestore.DocumentSnapshot.toObject(Unknown Source) at com.troychuinard.fanpolls.Fragment.PollFragment$6.onSuccess(PollFragment.java:534) at com.troychuinard.fanpolls.Fragment.PollFragment$6.onSuccess(PollFragment.java:531) at com.google.android.gms.tasks.zzj.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5527) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

1 answer

  • answered 2018-02-13 03:50 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() {}