简体   繁体   中英

Getting Error: Index 3 out of bounds for length 3 Error | Even though it should be within Array Bounds

I have a quiz app I am trying to create for a college project and in the previous screen, I ask how many questions the user would like to do for the quiz. From there I randomly pick out 3 questions from the question database as you can see below.

        try {
            connection = DriverManager.getConnection("jdbc:sqlite:src/quiz");
            PreparedStatement addQuestion = connection.prepareStatement("SELECT question_ID FROM questions");
            rs = addQuestion.executeQuery();
            ArrayList<Integer> quizID = new ArrayList<>();
            while (rs.next()) {
                quizID.add(rs.getInt("question_ID"));
            }
            for (int i = 0; i < amountQuestions; i++) {
                Random rand = new Random();
                questions.add(quizID.get(rand.nextInt(quizID.size())));
            }
        } catch (SQLException error) {
            System.err.println(error.getMessage());
        }

Then on the below code. I get those questions, then when the user clicks the button I increase count by 1 and rerun the method.

    private void renderQuestions() {
        System.out.println(questions);
        System.out.println(questions.size());
        System.out.println(count);
        try {
            connection = DriverManager.getConnection("jdbc:sqlite:src/quiz");
            PreparedStatement addQuestion = connection.prepareStatement("SELECT * FROM questions WHERE question_ID = ?");
            addQuestion.setInt(1, questions.get(count));
            rs = addQuestion.executeQuery();
            while (rs.next()) {
                ...
            }
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    userAnswer = buttonGroup.getSelection().getActionCommand();
                    count++;
                    renderQuestions();
                }
            });
        } catch (SQLException error) {
            System.err.println(error.getMessage());
        }
    }

I answer two questions and it's like the program finished the quiz for me. I don't get to click the next button before the error shows.

终端截图

You never checked whether count >= questions.size() , so of course you're getting an IndexOutOfBoundsException . Notice that in your ActionListener you incremented the count (which you used as the index) before rendering the question, so question 1 will use index 1, question 2 will use index 2 and so on. Remember that arrays and lists are zero-indexed , so the third question, which attempts to access index 3, naturally fails.

You probably want to move the count++ to after renderQuestions() and add a check somewhere for when you run out of questions (might be just me but I don't see you ever checking count ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM