我是java的新手,并且有一份我需要帮助的学校作业

时间:2017-04-28 01:23:22

标签: java eclipse tic-tac-toe

在这项任务中,我们被要求创建一个简单的游戏,如井字游戏或刽子手。我已经在eclipse上完成了我的tic-tac-toe程序,代码中没有显示错误,但是,当我尝试运行我的游戏时,这就出现了: 线程“main”java.lang.NullPointerException中的异常     at tictactoemain.TicTacToe.main(TicTacToe.java:18) 我想知道是否有人可以提出如何改进/解决这个问题的建议,下面是我的两个班级,第一个用于窗口,第二个用于实际游戏:

(抱歉凌乱的格式化,我对这个网站也是新手,以及XD)

package tictactoemain;


import javax.swing.JFrame;

/**
 * TicTacToe Assignment
 * @author Me
 *
 */
public class TicTacToe 

{

    public static void main(String[] args) 
    {
        JFrame ticTacToe = TicTacToeFrame();
        ticTacToe.setTitle("Ekin's Tic-Tac-Toe Game!");
        ticTacToe.setSize(600, 600);
        ticTacToe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ticTacToe.setLocationRelativeTo(null);
        ticTacToe.setVisible(true);

    }

    private static JFrame TicTacToeFrame() 
    {
        // method for TicTacToeFrame
        return null;
    }

}// end of TicTacToe
package tictactoemain;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

/**
* JFrame to hold TicTacToe board.
*/
public class TicTacToeFrame extends JFrame
{

    private static final long serialVersionUID = 1L;
// Indicate whose turn it is
   private char whoseTurn = 'X';
   private boolean gameOver = false;

   // Create cell grid
   private Cell[][] cells = new Cell[3][3];

   // Create a status label
   JLabel jlblStatus = new JLabel("X's turn to play");

   /**
    * No-argument Constructor
 * @return 
    */
   public TicTacToeFrame()
   {
       // Panel to hold cells
       JPanel panel = new JPanel(new GridLayout(3, 3, 0, 0));
       for (int i = 0; i < 3; i++)
           for (int j = 0; j < 3; j++)
               panel.add(cells[i][j] = new Cell());

       panel.setBorder(new LineBorder(Color.red, 1));
       jlblStatus.setBorder(new LineBorder(Color.yellow, 1));

       add(panel, BorderLayout.CENTER);
       add(jlblStatus, BorderLayout.SOUTH);
   }

   /**
    * Determine if game board is full.
    * @return True, if game board is full. Otherwise, false.
    */
    public boolean isFull()
    {
       for (int i = 0; i < 3; i++)
           for (int j = 0; j < 3; j++)
               if (cells[i][j].getToken() == ' ')
                   return false;
       return true;
    }

   /**
    * Determines if a given token has won.
    * @param token Token to test for winning
    * @return True, if the token has won. Otherwise, false.
    */
   public boolean isWon(char token)
   {
       // check rows
       for (int i = 0; i < 3; i++)
           if ((cells[i][0].getToken() == token)
                   && (cells[i][1].getToken() == token)
                   && (cells[i][2].getToken() == token))
           {
               return true;
           }

       // check columns
       for (int j = 0; j < 3; j++)
           if ((cells[0][j].getToken() == token)
               && (cells[1][j].getToken() == token)
               && (cells[2][j].getToken() == token))
           {
               return true;
           }
       // check diagonal
       if ((cells[0][0].getToken() == token)
               && (cells[1][1].getToken() == token)
               && (cells[2][2].getToken() == token))
           {
               return true;
           }

       if ((cells[0][2].getToken() == token)
               && (cells[1][1].getToken() == token)
               && (cells[2][0].getToken() == token))
           {
               return true;
           }

       return false;
   }

    /**
    * Defines a cell in a TicTacToe game board.
    */
    public class Cell extends JPanel
    {
       /**
         * 
         */
        private static final long serialVersionUID = 1L;
    // token of this cell
       private char token = ' ';

       /**
        * Constructor
        */
       public Cell()
       {
           setBorder(new LineBorder(Color.black, 1));
           addMouseListener(new MyMouseListener());
       }

       /**
        * Gets the token of the cell.
        * @return The token value of the cell.
        */
       public char getToken()
       {
           return token;
       }

       /**
        * Sets the token of the cell.
        * @param c Character to use as token value.
        */
       public void setToken(char c)
       {
           token = c;
           repaint();
       }

       @Override
       protected void paintComponent(Graphics g)
       {
           super.paintComponent(g);

           if (token == 'X')
           {
               g.drawLine(10, 10, getWidth() - 10, getHeight() - 10);
               g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10);
           }

           else if (token == 'O')
           {
               g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
           }
       }

       private class MyMouseListener extends MouseAdapter
       {
           @Override
           public void mouseClicked(MouseEvent e)
           {
               if (gameOver)
                   return;

               // if the cell is empty and the game is not over
               if (token == ' ' && whoseTurn != ' ')
                   setToken(whoseTurn);

               // Check game status
               if (isWon(whoseTurn))
               {
                   jlblStatus.setText(whoseTurn + " won! Game over!");
                   whoseTurn = ' ';
                   gameOver = true;
               }
               else if (isFull())
               {
                   jlblStatus.setText("Tie game! Game over!");
                   whoseTurn = ' ';
                   gameOver = true;
               }
               else
               {
                   whoseTurn = (whoseTurn == 'X') ? 'O' : 'X';
                   jlblStatus.setText(whoseTurn + "'s turn.");
               }
           }
       } // End class MyMouseListener
    } // End class Cell
} // End class TicTacToeFrame

3 个答案:

答案 0 :(得分:1)

JFrame ticTacToe = TicTacToeFrame();应为JFrame ticTacToe = new TicTacToeFrame();

按原样,您正在调用返回null的本地方法 - 之后的第一行将失败,因为它尝试针对null调用方法。

答案 1 :(得分:1)

摆脱这种方法

private static JFrame TicTacToeFrame() 
{
    // method for TicTacToeFrame
    return null;
}

您想要调用已经声明为

的构造函数
public TicTacToeFrame()
{
    ....
}

然后将其调用更改为

JFrame ticTacToe = new TicTacToeFrame();

答案 2 :(得分:0)

您不需要此方法来调用TicTacToeFrame类。

private static JFrame TicTacToeFrame() 
{
    // method for TicTacToeFrame
    return null;
}

摆脱它。并使用以下

替换JFrame ticTacToe = TicTacToeFrame();
    JFrame ticTacToe = new TicTacToeFrame();

通过调用TicTacToeFrame()方法,您显式返回null,这导致了NullPointer异常。