How to resolve java.lang.Indexoutofboundsexception: Index: 25, Size: 25

Asked

Viewed 6,616 times

0

I am developing an online questionnaire in JSP, where the user selects the radio of each question to be validated. Example: a questionnaire of 25 questions, if the user reaches 25 and clicks the previous 24 and gives the next one to 25, generates the error java.lang.IndexOutOfBoundsException: Index: 25, Size: 25. I was searching the Internet and found that this error may occur if a method tries to access a memory address in an array that does not exist, and has already reached its limit.

Class stretch Exam.class that the system accuses the error:

Configuration Works, however, from the error, if the user comes back on the last questions of the exam and click on the later

    for(int i=0;i<taken-1;i++){ 
        System.out.println("i ExamController: "+i+ "taken: "+taken);
        //o erro ocorre aqui!!!!
        if((userSelectionsList.get(i)-1)==correctAnswersList.get(i)){
            totalCorrect++;
            System.out.println("totalCorrect ExamController: "+totalCorrect);
        }
    }

Code snippet that calls the compute method: /The exam will be finished when the user finishes or when the time limit for the exam is long./

        else if("Finish Exam".equals(action)||( minute==0 && second==0))
        {   finish=true;
        /*Submetendo o Exame e Avaliação Resultado Exame. Quando o usuário clica no botão Concluir, ExamController
         *chama o método calculateResult () passando o objeto Exame, calculateResult ()*/
            int result=exam.calculateResult(exam,exam.questionList.size());                 
            request.setAttribute("result",result);              
            request.getRequestDispatcher("/jsps/result.jsp").forward(request,response);             
        }

Console error:

Question number 0 recovered Question number 1 recovered Number Question 2 recovered Question number 3 recovered Question number 4 recovered Question number 5 recovered Question number 6 recovered Question number 7 recovered Question number 8 recovered Number question 9 recovered Question number 10 recovered Question number 11 recovered Question number 12 recovered Question number 13 recovered Question number 14 recovered Question number 15 recovered Question number 16 recovered Question number 17 recovered Question number 18 recovered Question number 19 recovered Question number 20 recovered Question number 21 recovered Question number 22 recovered Question number 23 recovered Question number 24 recovered You clicked Previous Button Question number 23 recovered Question number 24 recovered You clicked Previous Button Question number 23 recovered You clicked Button Previous Question number 22 recovered Question number 23 recovered Question number 24 recovered

i Examcontroller: 0taken: 32 totalCorrect Examcontroller: 1 i Examcontroller: 1taken: 32 i Examcontroller: 2taken: 32 i Examcontroller: 3taken: 32 i Examcontroller: 4taken: 32 i Examcontroller: 5taken: 32 i Examcontroller: 6taken: 32 i Examcontroller: 7taken: 32 i Examcontroller: 8taken: 32 i Examcontroller: 9taken: 32 i Examcontroller: 10taken: 32 i Examcontroller: 11taken: 32 totalCorrect Examcontroller: 2 i Examcontroller: 12taken: 32 totalCorrect Examcontroller: 3 i Examcontroller: 13taken: 32 i Examcontroller: 14taken: 32 i Examcontroller: 15taken: 32 i Examcontroller: 16taken: 32 i Examcontroller: 17taken: 32 i Examcontroller: 18taken: 32 i Examcontroller: 19taken: 32 i Examcontroller: 20taken: 32

Jun 17, 2016 5:52:48 PM org.apache.Catalina.core.Standardwrappervalve invoke GRAVE: Servlet.service() for Servlet [br.com.tkcsapcd.quiz.controller.Examcontroller] in context with path [/Tkcsapcd_cadastros] threw Exception java.lang.Indexoutofboundsexception: Index: 25, Size: 25 at java.util.Arraylist.rangeCheck(Unknown Source) at java.util.Arraylist.get(Unknown Source) at br.com.tkcsapcd.quizz.Exam.calculateResult(Exam.java:128) at br.com.tkcsapcd.quiz.controller.ExamController.doPost(Examcontroller.java:127) at javax.servlet.http.HttpServlet.service(Httpservlet.java:646) at javax.servlet.http.HttpServlet.service(Httpservlet.java:727) at org.apache.Applicationfilterchain.internalDoFilter(Applicationfilterchain.java:303) at org.apache.Catalina.core.Applicationfilterchain.doFilter(Applicationfilterchain.java:208) at org.apache.Tomcat.websocket.server.WsFilter.doFilter(Wsfilter.java:52) at org.apache.Applicationfilterchain.internalDoFilter(Applicationfilterchain.java:241) at org.apache.Catalina.core.Applicationfilterchain.doFilter(Applicationfilterchain.java:208) at org.apache.Catalina.core.Standardwrappervalve.invoke(Standardwrappervalve.java:220) at org.apache.Catalina.core.Standardcontextvalve.invoke(Standardcontextvalve.java:122) at org.apache.Catalina.authenticator.Authenticatorbase.invoke(Authenticatorbase.java:501) at org.apache.Catalina.core.Standardhostvalve.invoke(Standardhostvalve.java:170) at org.apache.Catalina.valves.Errorreportvalve.invoke(Errorreportvalve.java:98) at org.apache.Catalina.valves.Accesslogvalve.invoke(Accesslogvalve.java:950) at org.apache.Catalina.core.Standardenginevalve.invoke(Standardenginevalve.java:116) at org.apache.Catalina.connector.Coyoteadapter.service(Coyoteadapter.java:408) at org.apache.Coyote.http11.Abstracthttp11processor.process(Abstracthttp11processor.java:1040) at org.apache.Coyote.Abstractprotocol$Abstractconnectionhandler.process(Abstractprotocol.java:607) at org.apache.Tomcat.util.net.Jioendpoint$Socketprocessor.run(Jioendpoint.java:315) at java.util.Concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.Concurrent.Threadpoolexecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

How could I fix this?

1 answer

3

According to the description of the error itself, you have a size 25 list and are trying to access position 25.

The list, however, has the positions from 0 to 24. That is, you are trying to access an index that is outside the limit of the list.

You need to put a breakpoint on that stretch:

if((userSelectionsList.get(i)-1)==correctAnswersList.get(i)){

Then you should inspect what happens when you come back a question and then move on. Probably what’s happening is this:

  1. You have a correct answer counter
  2. You are making a loop to know how many questions the user has hit.
  3. The loop is programmed to repeat 25 times.
  4. When you return a question, you should decrease the amount of questions asked and decrease the amount of correct answers. Because in this situation the user is answering a question already answered.

However, it seems that when a question comes back and answers it again, the counter keeps growing. When you enter the loop, the number of questions answered exceeds 25.

When inspecting the code you need to check the following information:

  1. The size of the lists userSelectionsList and correctAnswersList. This way you will know which of the two lists is giving the error.
  2. The value of the variable Taken. This variable needs to have the same value as the number of questions asked. In this case, I believe you do not need this: Taken-1. I believe you did this to avoid error.
  3. See also how these two lists are being filled in. Remember, the size of the correct list should be at most the amount of questions asked.

I hope I helped. If you cannot solve, post here the information cited in this reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.