My assignment is to create a tic-tac-toe game. I thought my code looked good, and it compiles, but it returns:

"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)

My question: What am I doing that causes this error? My teacher gave me the GUI class without the source code. Also I am not allowed to replace/modify the main or GUI classes. Here is my TicTacToe logic class:

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

}
有帮助吗?

解决方案

Your problem is that you don't initialize marks. In the constructor you declare a local variable called marks, but so your global one stays null. Change your constructor like this:

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

其他提示

You're shadowing the marks and the red variables by re-declaring them in the constructor. This will initialize the variable that is local to the constructor, leaving the instance field null. Solution: don't re-declare the variable.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top