Tic Tac Toe程序返回java.lang.NullPointerException
我的任务是制作一个井字游戏。我认为我的代码看起来不错,可以编译,但它返回:Tic Tac Toe程序返回java.lang.NullPointerException,java,multidimensional-array,nullpointerexception,tic-tac-toe,bluej,Java,Multidimensional Array,Nullpointerexception,Tic Tac Toe,Bluej,我的任务是制作一个井字游戏。我认为我的代码看起来不错,可以编译,但它返回: "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at TicTacModel.squareClicked(TicTacModel.java:33) at TicTacGUI$ButtonListener.gridButtonClicked(TicTacGUI.java:79) at TicTacGUI$ButtonListene
"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类。
这是我的TictaToe逻辑课程:
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;
}
}
公共类模型
{
//实例变量。
//您需要包括两个二维阵列。
//-一个二维字符串对象数组,可记住“游戏板”上的标记,
//-另一个2-D布尔数组,它将告诉GUI用红色标记哪些方块。(真=红色)
专用字符串[][]标记;
私有布尔[][]红色;
//您的二维数组中没有完整的整数值,因此java不会提供可靠的默认值。
//使用构造函数用适当的默认值填充它们。
公共模型()
{
布尔[][]红色={{false,false,false},
{假,假,假},
{假,假,假};
字符串[][]标记={{{,'',''},
{" ", " ", " "},
{" ", " ", " "}};
}
//每次单击正方形时,GUI都会发送单击的位置
//以及标记(“X”或“O”)。将此信息记录在您的实例2-D字符串数组中。
公共虚线(整行、整列、字符串标记)
{
标记[列][行]=标记;
}
//从GUI确定字符串的二维数组中是否充满了“X”和“O”。
//如果已满则返回true,否则返回false。
公共布尔值isFull()
{
int x,y=0;
对于(x=0;x通过在构造函数中重新声明标记和红色变量来隐藏它们。这将初始化构造函数本地的变量,使实例字段为空。解决方案:不要重新声明变量。通过在构造函数中重新声明标记和红色变量来隐藏它们。这将导致将初始化构造函数的局部变量,将实例字段保留为空。解决方案:不要重新声明该变量。您的问题是您没有初始化标记。在构造函数中,您声明了一个名为标记的局部变量,但全局变量保持为空。按如下方式更改构造函数:
public TicTacModel()
{
red = {{false, false, false},
{false, false, false},
{false, false, false}};
marks = {{" ", " ", " "},
{" ", " ", " "},
{" ", " ", " "}};
}
您的问题是没有初始化标记
。在构造函数中,您声明了一个名为标记
的局部变量,但全局变量保持为空。请按如下方式更改构造函数:
public TicTacModel()
{
red = {{false, false, false},
{false, false, false},
{false, false, false}};
marks = {{" ", " ", " "},
{" ", " ", " "},
{" ", " ", " "}};
}
好的,当我将代码更改为该值时,红色
和标记
变量声明前面会出现一个“表达式非法开始”错误。为了解决这个问题,我在数组类的开头声明变量时,只给数组分配了变量。谢谢!好的,当我将代码更改为该值时,“表达式非法开始”在红色
和标记
变量声明前面出现错误。为了解决此问题,我在数组类的开头声明时,只为数组分配了变量。谢谢!