gpt4 book ai didi

java - Tic-Tac-Toe 程序返回 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 18:40:05 26 4
gpt4 key购买 nike

我的任务是创建一个井字棋游戏。我认为我的代码看起来不错,并且可以编译,但它返回:

"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 类(class),但没有源代码。另外,我也不允许替换/修改主类或 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;
}

}

最佳答案

您的问题是您没有初始化标记。在构造函数中,您声明了一个名为 marks 的局部变量,但全局变量仍为 null。像这样更改你的构造函数:

public TicTacModel()
{
red = {{false, false, false},
{false, false, false},
{false, false, false}};
marks = {{" ", " ", " "},
{" ", " ", " "},
{" ", " ", " "}};
}

关于java - Tic-Tac-Toe 程序返回 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20294022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com