我的任务是创造一个井字游戏。我认为我的代码看起来很好,并且编译,但它返回:
"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at TicTacModel.squareClicked(TicTacModel.java:33)
at TicTacGUI$ButtonListener.gridButtonClicked(TicTacGUI.java:79)
at TicTacGUI$ButtonListener.actionPerformed(TicTacGUI.java:53)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
我的问题:我在做什么导致此错误? 我的老师给了我没有源代码的GUI类。此外,我不允许替换/修改主类或GUI类。 这是我的TicTacToe逻辑类:
public class TicTacModel
{
// Instance Variables.
// You will need to include two 2-D Arrays.
// - one 2-D Array of String objects which remembers the marks on the "game board",
// - another 2-D Array of booleans which will tell the GUI what squares to mark in red. (true = red)
private String[][] marks;
private boolean[][] red;
//Your 2-D Arrays are not full of integer values, so java will not supply reliable defaults.
//Use the constructor to fill them with approprate default values.
public TicTacModel()
{
boolean[][] red = {{false, false, false},
{false, false, false},
{false, false, false}};
String[][] marks = {{" ", " ", " "},
{" ", " ", " "},
{" ", " ", " "}};
}
//Each time a square is clicked, the GUI will send the location of the click
//and the mark made ("X" or "O"). Record this informaion in your instance 2-D Array of Strings.
public void squareClicked(int row, int col, String mark)
{
marks[col][row] = mark;
}
//Determine if the 2-D Array of Strings is full of "X"s and "O"s from the GUI.
//Return true if it is full, false otherwise.
public boolean isFull()
{
int x, y = 0;
for (x = 0; x <= 2; x++)
{
for (y = 0; y <= 2; y++)
{
if (marks[x][y] == " ")
{
return false;
}
}
}
return true;
}
//This method will require the most code.
//Check all possible ways there could be a winner.
//Return true if there is a winner, false otherwise.
//Set the corresponding elements of the instance boolean 2-D Array to true.
public boolean isWinner()
{
if (marks[0][0] == marks[1][0] && marks[0][0] == marks[2][0])
{
red[0][0] = true;
red[1][0] = true;
red[2][0] = true;
return true;
}
else if (marks[0][0] == marks[0][1] && marks[0][0] == marks[0][2])
{
red[0][0] = true;
red[0][1] = true;
red[0][2] = true;
return true;
}
else if (marks[0][0] == marks[1][1] && marks[0][0] == marks[2][2])
{
red[0][0] = true;
red[1][1] = true;
red[2][2] = true;
return true;
}
else if (marks[0][1] == marks[1][1] && marks[0][1] == marks[2][1])
{
red[0][1] = true;
red[1][1] = true;
red[2][1] = true;
return true;
}
else if (marks[0][2] == marks[1][2] && marks[0][2] == marks[2][2])
{
red[0][2] = true;
red[1][2] = true;
red[2][2] = true;
return true;
}
else if (marks[1][0] == marks[1][1] && marks[1][0] == marks[1][2])
{
red[1][0] = true;
red[1][1] = true;
red[1][2] = true;
return true;
}
else if (marks[2][0] == marks[2][1] && marks[2][0] == marks[2][2])
{
red[2][0] = true;
red[2][1] = true;
red[2][2] = true;
return true;
}
else if (marks[2][0] == marks[1][1] && marks[2][0] == marks[0][2])
{
red[2][0] = true;
red[1][1] = true;
red[0][2] = true;
return true;
}
else return false;
}
//A getter method which returns the instance 2-D Array of booleans.
//Corresponding elements containing 'true' will be shaded red by the GUI.
public boolean[][] getRedSquares()
{
return red;
}
}
答案 0 :(得分:3)
您的问题是您没有初始化marks
。在构造函数中,您声明了一个名为marks
的局部变量,但是您的全局变量保持为null。像这样改变你的构造函数:
public TicTacModel()
{
red = {{false, false, false},
{false, false, false},
{false, false, false}};
marks = {{" ", " ", " "},
{" ", " ", " "},
{" ", " ", " "}};
}
答案 1 :(得分:1)
您通过在构造函数中重新声明标记和红色变量来隐藏它们。这将初始化构造函数的本地变量,将实例字段保留为null。解决方案:不要重新声明变量。