title: How to display first Question in arrayList with retrofit API Quiz App
body: <p>I have a Quiz project and want to display multiple choice questions, here I use two activities, one fragment and retrofit API, but when I want to display the first question it's always an error and after the next one comes the second question</p>
  
  <p>first activity i called StartQuestion in this Activity i use retrofit to call data From API</p>
  
  <pre><code>public class StartQuestionActivity extends AppCompatActivity implements View.OnClickListener {
  
  Button start;
  ArrayList&lt;AssessmentResponse.Soal&gt; dataSoalList;
  SendData mSend;
  String listSoal;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_start_question);
  
  
      fetchRemote();
      start = findViewById(R.id.btnStart);
      start.setOnClickListener(this);
  }
   @Override
      public void onClick(View view) {
          Intent intent= new Intent(this,SelfLearningQuizActivity.class);
          intent.putExtra("listSoal",listSoal);
          startActivity(intent);
      }
  </code></pre>
  
  <p>in here i called retrofit API</p>
  
  <pre><code> public void fetchRemote(){
  
      ApiCaller apiCaller = new ApiCaller();
  
      apiCaller.withListener(new OnApiListener&lt;AssessmentResponse&gt;() {
          @Override
          public void onApiSuccess(AssessmentResponse assessmentResponse) {
              dataSoalList = assessmentResponse.getResponData().getSoal();
              Gson gson = new Gson();
              listSoal = gson.toJson(dataSoalList);
  
          }
  
          @Override
          public void OnApiFailur(@NotNull AppError error) {
              Toast.makeText(getApplicationContext(),"Gagal",Toast.LENGTH_SHORT).show();
  
          }
      }).getAssesment(requestBody.build(),getApplicationContext());
  }
  </code></pre>
  
  <p>after the data I got from StartActivity I made one activity and one fragment to display the question. I Called SelfLeraningQuizActivity and SelfLearningQuizFragment</p>
  
  <p>SelfLearningQuizActivty code:</p>
  
  <pre><code>public class SelfLearningQuizActivity extends AppCompatActivity implements View.OnClickListener, StartQuestionActivity.SendData/*, SelfLearningQuizFragment.OnRadioGroupSelectedListener*/ {
  
  private int mfragmentIndex=0;
  private ViewPager viewPager;
  public static int obtainedScore=0;
  private Question currentQuestion;
  private List&lt;Question&gt; questionsList;
  private HashMap&lt;String,Object&gt; hashMap;
  private ArrayList&lt;AssessmentResponse.Soal&gt; dataSoalList = new ArrayList&lt;&gt;() ;
  private Button btnBack,btnNext;
  
  private QuestionCallbacks mQuestionCallbacks;
  private List&lt;QuestionCallbacks&gt; questionCallbacks = new ArrayList&lt;&gt;();
  
  @Override
  public void onSend(ArrayList&lt;AssessmentResponse.Soal&gt; listSoal) {
      /*dataSoalList = listSoal;
      Toast.makeText(this,dataSoalList.size(),Toast.LENGTH_LONG).show();*/
  }
  
   @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_self_learning_quiz);
      btnBack = findViewById(R.id.btnBack);
      btnNext = findViewById(R.id.btnNext);
  
      viewPager = findViewById(R.id.view_pager);
  
      btnBack.setOnClickListener(this);
      btnNext.setOnClickListener(this);
  
      dataSoalList = new ArrayList&lt;&gt;();
      String listSoalString = getIntent().getStringExtra("listSoal");
      Gson gson = new Gson();
      Type type = new TypeToken&lt;ArrayList&lt;AssessmentResponse.Soal&gt;&gt;(){}.getType();
      ArrayList&lt;AssessmentResponse.Soal&gt; soalList = gson.fromJson(listSoalString, type);
      dataSoalList.addAll(soalList);
      setupViewPager(viewPager);
  </code></pre>
  
  <p>if i use the code cant display first Question and nullpointer</p>
  
  <pre><code>  if(mfragmentIndex == 0){
              questionCallbacks.get(mfragmentIndex).onSetQuestionData(dataSoalList.get(mfragmentIndex));
  
   }
      }
  </code></pre>
  
  <p>and this error code display for first Question:</p>
  
  <pre><code> java.lang.RuntimeException: Unable to start activity ComponentInfo{brismart.bri.co.id/brismart.bri.co.id.SelfLearningQuizActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2708)
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
                    at android.app.ActivityThread.-wrap12(ActivityThread.java)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:160)
                    at android.app.ActivityThread.main(ActivityThread.java:6248)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:788)
  </code></pre>
  
  <p>if I don't use the code display the first question 
  the result :<a href="https://i.stack.imgur.com/lhNrh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lhNrh.png" alt="enter image description here"></a></p>
  
  <p>first Question can't display but Second Question display :
  <a href="https://i.stack.imgur.com/EGXdV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EGXdV.png" alt="enter image description here"></a></p>
  
  <p>Set ViewPager and Onclick:</p>
  
  <pre><code>private void setupViewPager(ViewPager viewPager) {
  
      ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
  
      int index = 0;
      for (AssessmentResponse.Soal soal:dataSoalList) {
          SelfLearningQuizFragment selfLearningQuizFragment = SelfLearningQuizFragment.newInstance(String.valueOf(index++));
          mQuestionCallbacks = selfLearningQuizFragment;
          adapter.addFrag(selfLearningQuizFragment,"Question"+index++);
          questionCallbacks.add(mQuestionCallbacks);
      }
  
      viewPager.setOffscreenPageLimit(adapter.getCount());
      viewPager.setAdapter(adapter);
  }
  
  @Override
  public void onClick(View view) {
      if(view == btnBack){
          mfragmentIndex--;
          viewPager.setCurrentItem(mfragmentIndex);
      }else if(view == btnNext){
          mfragmentIndex++;
          viewPager.setCurrentItem(mfragmentIndex);
          questionCallbacks.get(mfragmentIndex).onSetQuestionData(dataSoalList.get(mfragmentIndex));
      }
  }
  </code></pre>
  
  <p>I use interface to send data Question from Activity to fragment</p>
  
  <pre><code>public interface QuestionCallbacks{
      void onSetQuestionData(AssessmentResponse.Soal dataSoalList);
  //        void onUserGoToNext(int index);
  }
  </code></pre>
  
  <p>and this i display Question in SelfLearningQuizFragment</p>
  
  <pre><code>public class SelfLearningQuizFragment extends Fragment implements SelfLearningQuizActivity.QuestionCallbacks {
  private static final String ARG_QUESTION_ID = "question_id";
  private AssessmentResponse.Soal dataSoal;
  
  private int answeredQsNo=0;
  
  private TextView txtQuestion,tvNumberOfQuestions,totalQuestion;
  private RadioButton rbtnA, rbtnB, rbtnC,rbtnD;
  LinearLayout parent;
  Button next;
  RadioGroup radioGroup;
  private int indexSoal=0;
  private int rightAnswer=0;
  private int wrongAnswer=0;
  
  public SelfLearningQuizFragment(){
  
  }
  public static SelfLearningQuizFragment newInstance(String questionId1) {
      final SelfLearningQuizFragment fragment = new SelfLearningQuizFragment();
      final Bundle args = new Bundle();
      args.putString(ARG_QUESTION_ID,questionId1);
      fragment.setArguments(args);
      return fragment;
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_self_learning_quiz, container, false);
  
      totalQuestion = view.findViewById(R.id.totalQuestion);
      tvNumberOfQuestions = view.findViewById(R.id.tvNumberOfQuestions);
      txtQuestion=view.findViewById(R.id.tvQuestion);
      rbtnA=view.findViewById(R.id.opt1);
      rbtnB=view.findViewById(R.id.opt2);
      rbtnC=view.findViewById(R.id.opt3);
      rbtnD=view.findViewById(R.id.opt4);
  //if i use on setQustiondata is nullpointer Textview
  //        onSetQuestionData(dataSoal);
  
  
  
      @Override
      public void onSetQuestionData(AssessmentResponse.Soal dataSoalList) {
  
  
           dataSoal = dataSoalList;
      //        Toast.makeText(getActivity(), dataSoalList.getPertanyaan(), Toast.LENGTH_SHORT).show();
          txtQuestion.setText(dataSoal.getPertanyaan());
          tvNumberOfQuestions.setText(String.valueOf(indexSoal+1));
          rbtnA.setText(dataSoal.getOptAnswer().get(0).getTextAnswer());
          rbtnB.setText(dataSoal.getOptAnswer().get(1).getTextAnswer());
          rbtnC.setText(dataSoal.getOptAnswer().get(2).getTextAnswer());
          rbtnD.setText(dataSoal.getOptAnswer().get(3).getTextAnswer());
          radioGroup.setTag("true");
      }
  </code></pre>
  
  <p>any idea for this ?
  thanks you for help</p>