0

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)
Mutating Algorithm
  • 2,604
  • 2
  • 29
  • 66

2 Answers2

0

Your problem is most likely here:

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

You are incrementing i by 4 each iteration of the for loop. Which means the first loop you are accessing the 0, 1, 2, and 3 elements of the lines Vector. The next loop you are accessing 4, 5, 6, 7. Next you are accessing 8, 9, 10, 11. The lines Vector may not have that many elements in it.

Josh Chappelle
  • 1,558
  • 2
  • 15
  • 37
0

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));
    }
}
Josh Chappelle
  • 1,558
  • 2
  • 15
  • 37