javaappletawt-eventqueue

Java Applet AWT-EventQueue-1 ArrayIndexOutOfBoundsException


I created a Vector called lines that stores the coordinate points of the end points of drawn line. It accomplishes the task that I want by drawing lines from the stored coordinates but produces the following exception:

Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 10
    at java.util.Vector.get(Vector.java:748)
    at DrawPad.paint(DrawPad.java:149)
    at java.awt.Container.update(Container.java:1995)
    at sun.awt.RepaintArea.updateComponent(RepaintArea.java:255)
    at sun.awt.RepaintArea.paint(RepaintArea.java:232)
    at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:358)
    at java.awt.Component.dispatchEventImpl(Component.java:4967)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Solution

  • You need to check to see if the element exists before calling lines.get(i). Also, use lines.size() instead of lines.capacity().

    for(int i = 0; i < lines.size(); i += 4)
    {
        if((i+3) < lines.size())
        {
            g.drawLine(lines.get(i), lines.get(i + 1), lines.get(i + 2), lines.get(i + 3));
        }
    }