Question

I am making a quiz-game that will read a number of questions and their respective answers from a text file. These are first put into an ArrayList but then each question is to be converted into a separate question-object. It is during this part of the program I receive an IndexOutOfBoundsException.

The textfile is formatted in the following way:

Question
Correct Answer
Wrong Answer1
Wrong Answer2
Wrong Answer3
(emptyline)
Question2
...
...
...

The textfile is handled in a class called IO using a bufferedReader. The questions are stored in an ArrayList> so that each question is stored separate. In a class called questions I have a method used for creating objects from the ArrayList.

The code looks like this:

public class Questions
{

private ArrayList<ArrayList<String>> originalList;
private ArrayList<SingleQuestion> newList;
private ArrayList<SingleQuestion> objectList;
private IO io;



public Questions(){
    io = new IO();  //Creates a new instance of IO.
    objectList = new ArrayList<SingleQuestion>();
    createQuestions();
}

public void createQuestions(){

    originalList = io.getArray();

    for(int i = 0; i < originalList.size(); i++)
    {
        objectList.add(new SingleQuestion(originalList.get(i)));
    }

}

The SingleQuestion class constructor like this: public class SingleQuestion { private String question; private String correctAnswer; private String answer2; private String answer3; private String answer4;

public SingleQuestion(ArrayList<String> questionArray){

    this.question = questionArray.get(0);
    this.correctAnswer = questionArray.get(1);
    this.answer2 = questionArray.get(3);
    this.answer3 = questionArray.get(4);
    this.answer4 = questionArray.get(5);
}

When the code reaches the very end of SingleQuestions constructor I get an IndexOutOfBoundsException.

The error message looks like the following:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
 at java.util.ArrayList.rangeCheck(Unknown Source)
 at java.util.ArrayList.get(Unknown Source)
 at MVC_test.SingleQuestion.<init>(SingleQuestion.java:21)
 at MVC_test.Questions.createQuestions(Questions.java:47)
 at MVC_test.Questions.<init>(Questions.java:20)
 at MVC_test.GModel.<init>(GModel.java:23)
 at MVC_test.GMain.main(GMain.java:7)
Was it helpful?

Solution

this.question = questionArray.get(0);
this.correctAnswer = questionArray.get(1);
this.answer2 = questionArray.get(3);
this.answer3 = questionArray.get(4);
this.answer4 = questionArray.get(5);

should be

this.question = questionArray.get(0);
this.correctAnswer = questionArray.get(1);
this.answer2 = questionArray.get(2);
this.answer3 = questionArray.get(3);
this.answer4 = questionArray.get(4);

OTHER TIPS

You skipped questionArray.get(2). The index will only go up to 4.

Looks like you forgot a number...

public SingleQuestion(ArrayList<String> questionArray){

    this.question = questionArray.get(0);
    this.correctAnswer = questionArray.get(1);
    this.answer2 = questionArray.get(3);
    this.answer3 = questionArray.get(4);
    this.answer4 = questionArray.get(5);
}

The list has only five entries, so questionArray.get(5); will give you an exception. You never use the third element (index 2).

Java methods and classes are typically 0-indexed: that means that the first element is element number 0, second element is element number 1, etc. So for your ArrayList, where you have 5 elements (it is apparently of size() == 5), the last element is in fact 5-1, or 4. You are getting the IndexOutOfBoundsException because of this line:

this.answer4 = questionArray.get(5);

What you want is:

this.answer4 = questionArray.get(4);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top