Search code examples
javaindexoutofboundsexception

When LinkedList size is -1


I'm facing this exception, in a java 1.7 project:

Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: -1
   at java.util.LinkedList.checkPositionIndex(LinkedList.java:558) ~[na:1.7.0_251]
   at java.util.LinkedList.listIterator(LinkedList.java:865) ~[na:1.7.0_251]
   at java.util.AbstractList.listIterator(AbstractList.java:299) ~[na:1.7.0_251]
   at java.util.AbstractSequentialList.iterator(AbstractSequentialList.java:239) ~[na:1.7.0_251]
   at com.project.exceptions.handlers.JsfExceptionHandler.handle(JsfExceptionHandler.java:74) ~[project-0.0.1-SNAPSHOT.jar:na]
   at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119) ~[jsf-impl-2.1.13-redhat-1.jar!/:2.1.13-redhat-1]
   at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) ~[jsf-impl-2.1.13-redhat-1.jar!/:2.1.13-redhat-1]
   at org.springframework.faces.webflow.FlowLifecycle.render(FlowLifecycle.java:80) ~[spring-faces-2.3.2.RELEASE.jar:2.3.2.RELEASE]
   at org.springframework.faces.webflow.JsfView.render(JsfView.java:89) ~[spring-faces-2.3.2.RELEASE.jar:2.3.2.RELEASE]
   at org.springframework.webflow.engine.ViewState.render(ViewState.java:296) ~[spring-webflow-2.3.2.RELEASE.jar:2.3.2.RELEASE]
   at org.springframework.webflow.engine.ViewState.resume(ViewState.java:207) ~[spring-webflow-2.3.2.RELEASE.jar:2.3.2.RELEASE]
   at org.springframework.webflow.engine.Flow.resume(Flow.java:545) [spring-webflow-2.3.2.RELEASE.jar:2.3.2.RELEASE]
   at org.springframework.webflow.engine.impl.FlowExecutionImpl.resume(FlowExecutionImpl.java:258) [spring-webflow-2.3.2.RELEASE.jar:2.3.2.RELEASE]
   ... 62 common frames omitted

but i can't figure out in which situation the size of a LinkedList could be -1, isn't suppose to be a positive number? Any suggestion?

Custom Exception Handler code (row 74 is the for)

    public class JsfExceptionHandler extends ExceptionHandlerWrapper {
        ...
        @Override
        public void handle() throws FacesException {
            FacesContext fc = ContextUtils.getFacesContextInstance();
            for (Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
               ...
            }
        }
    }

Solution

  • I am able to reproduce the size of a LinkedList being -1 if I run this code couple of times:

             try {
                LinkedList<Integer> integers = new LinkedList<>();
                integers.add(1);
                new Thread(() -> {
                    try {
                        Thread.sleep(1950);
                        integers.removeFirst();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }).start();
    
                Thread.sleep(1950);
                integers.removeFirst();
    
    
                System.out.println(integers.size());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    

    As LinkedList is not thread-safe, trying to remove some elements from the LinkedList instance simultaneously from different threads might lead to the size being -1.