Search code examples
androidandroid-studiomethodsandroid-asynctasksynchronization

Problem in parsing JSON data using volley library


This is a simple program to gain the JSON data from the internet. answerWithAsyncTask() is an interface that ensures that all the downloaded data will only be added to questionArrayList when the download is complete.

Error: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0

 private List<Question> questionList;
 protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

//Few findViewbyId's here. Ignoring them

        questionList = new QuestionBank().getQuestions(new answerWithAsyncTask() {
            @Override
            public void asyncMe(ArrayList<Question> questionArrayList) {

                questionTextview.setText(questionArrayList.get(currentQuestionIndex).getQuestionId());
            }
        });
                 updateQuestion();     //This is the newly added line
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.prev_button:
                updateQuestion();
                break;
        }
    }
    private void updateQuestion() {
        String question = questionList.get(1).getQuestionId();
        questionTextview.setText(question);
    }

UPDATE This is my getQuestions method.

String url ="https://raw.githubusercontent.com/curiousily/simple-quiz/master/script/statements-data.json";
    private ArrayList<Question> questionArrayList= new ArrayList<>();

    public List<Question> getQuestions (final answerWithAsyncTask callback){
        JsonArrayRequest jsonArrayRequest =new JsonArrayRequest(Request.Method.GET, url, (String) null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        for(int i=0;i<response.length();i++){
                            Question question = new Question();
                            try {
                                question.setQuestionId(response.getJSONArray(i).getString(0));
                                question.setTorF(response.getJSONArray(i).getBoolean(1));
                                questionArrayList.add(question);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                        if(null != callback) callback.asyncMe(questionArrayList);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                });
        AppController.getInstance().addToRequestQueue(jsonArrayRequest);
        return questionArrayList;
    }

and this my interface answerWithAsyncTask


public interface answerWithAsyncTask {
    void asyncMe(ArrayList<Question> arrayList);
}


Solution

  • You are getting this error because

     private List<Question> questionList;
    

    is empty. In your first code, you are not getting any error because you are not calling updateQuestion() which will try to fetch data from an empty list. The error is in 2nd code because it's trying to access that empty list. Your

     return questionArrayList;
    

    is not returning data to questionList. Here, you are trying to do interface callback, to implement it properly, please look at this answer and change your code accordingly, Java Interface Callback