Java 小程序 AWT-EventQueue-1 ArrayIndexOutOfBoundsException
Java Applet AWT-EventQueue-1 ArrayIndexOutOfBoundsException
我创建了一个名为 lines 的 Vector,用于存储绘制线端点的坐标点。它通过从存储的坐标绘制线条来完成我想要的任务,但会产生以下异常:
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.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:709)
at java.awt.EventQueue.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.run(EventQueue.java:731)
at java.awt.EventQueue.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)
您的问题最有可能在这里:
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));
您在 for 循环的每次迭代中将 i 递增 4。这意味着您正在访问 Vector 行的 0、1、2 和 3 元素的第一个循环。下一个循环您将访问 4、5、6、7。接下来您将访问 8、9、10、11。Vector 行中可能没有那么多元素。
您需要在调用 lines.get(i) 之前检查该元素是否存在。另外,使用 lines.size() 而不是 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));
}
}
我创建了一个名为 lines 的 Vector,用于存储绘制线端点的坐标点。它通过从存储的坐标绘制线条来完成我想要的任务,但会产生以下异常:
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.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:709)
at java.awt.EventQueue.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.run(EventQueue.java:731)
at java.awt.EventQueue.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)
您的问题最有可能在这里:
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));
您在 for 循环的每次迭代中将 i 递增 4。这意味着您正在访问 Vector 行的 0、1、2 和 3 元素的第一个循环。下一个循环您将访问 4、5、6、7。接下来您将访问 8、9、10、11。Vector 行中可能没有那么多元素。
您需要在调用 lines.get(i) 之前检查该元素是否存在。另外,使用 lines.size() 而不是 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));
}
}