我正在开展一个学校项目,该项目涉及制作尺寸可调的井字游戏。我在按钮处理代码中遇到错误:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
at tictac.TicTacEvent.b(TicTacEvent.java:109)
at tictac.TicTacEvent.actionPerformed(TicTacEvent.java:67)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
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:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
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:80)
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)
我通常认为这是由于我在数组的参数之外调用索引引起的,但我无法在我的代码中找到它:
void b(int n)
{
// code to change n to array indices
// indice positions
int x = 0;
int y = 0;
int counter = 1;
for (int i = 0; i <= (d - 1); i++)
{
for (int j = 0; j <= (d - 1); j++)
{
if (n == counter)
{
x = j;
y = i;
gui.blank1.setText("" + x + " " + y + "\n"); // debugging
break; // values found, exit loop
}
else
{
// increment counter
counter ++;
}
}
}
counter = 1;
clicks++;
if ((clicks % 2) == 1) // not divisible by two
{
gui.boxes[x][y].setIcon(a);
check[x][y] = 1;
}
else
{
gui.boxes[x][y].setIcon(b);
check[x][y] = 2;
}
winner();
}
当我呼叫gui.boxes[x][y]
时,以及当我呼叫b(n)
功能时,会发生错误
谢谢!
答案 0 :(得分:1)
你的stacktrace说超出范围的索引是0.为了可能,数组的长度必须为零。因此,gui.boxes.length
为零或x
的某个值,gui.boxes[x].length
为零。检查两者。