【问题标题】:Tic-Tac-Toe program returns java.lang.NullPointerException井字游戏程序返回 java.lang.NullPointerException
【发布时间】:2013-12-16 03:18:40
【问题描述】:

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

"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 类。 这是我的井字游戏逻辑类:

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;
}

}

【问题讨论】:

    标签: java multidimensional-array nullpointerexception tic-tac-toe bluej


    【解决方案1】:

    你的问题是你没有初始化marks。在构造函数中,您声明了一个名为marks 的局部变量,但因此您的全局变量保持为空。像这样改变你的构造函数:

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

    【讨论】:

    • 好的,当我将代码更改为该代码时,redmarks 变量声明前面出现“表达式非法开始”错误。
    • 为了解决这个问题,当我在类的开头声明它们时,我只是将变量分配给数组。谢谢!
    【解决方案2】:

    您通过在构造函数中重新声明标记和红色变量来隐藏它们。这将初始化构造函数的本地变量,使实例字段为空。解决方法:不要重新声明变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 2015-06-12
      • 2015-01-08
      相关资源
      最近更新 更多