我是java新手,有一个学校作业需要帮助

我是java新手,有一个学校作业需要帮助,java,eclipse,tic-tac-toe,Java,Eclipse,Tic Tac Toe,在这项任务中,我们被要求创建一个简单的游戏,如tic-tac-toe或hangman。我已经在eclipse上完成了tic-tac-toe程序,代码中没有显示任何错误,但是,当我尝试运行游戏时,出现以下情况: 线程“main”java.lang.NullPointerException中出现异常 在tictactoemain.TicTacToe.main(TicTacToe.java:18) 我想知道是否有人可以就如何改进/修复此问题提供建议,以下是我的两个课程:第一个是针对窗口的课程,第二个是

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

(很抱歉格式混乱,我对这个网站也很陌生)


package-tictactoemain;
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.border.LineBorder;
/**
*JFrame用于固定Tictaoe板。
*/
公共类TicTacToeFrame扩展了JFrame
{
私有静态最终长serialVersionUID=1L;
//指明该轮到谁了
私有字符whoseTurn='X';
私有布尔gameOver=false;
//创建单元网格
专用小区[][]小区=新小区[3][3];
//创建状态标签
JLabel jlblStatus=新JLabel(“轮到X玩”);
/**
*无参数构造函数
*@返回
*/
公共TicTacToeFrame()
{
//放置电池的面板
JPanel面板=新JPanel(新网格布局(3,3,0,0));
对于(int i=0;i<3;i++)
对于(int j=0;j<3;j++)
panel.add(单元格[i][j]=新单元格());
面板。设置顺序(新线条边框(颜色为红色,1));
jlblStatus.setBorder(新线条边框(颜色为黄色,1));
添加(面板、边框布局、中心);
添加(jlblStatus,BorderLayout.SOUTH);
}
/**
*确定游戏板是否已满。
*@return True,如果游戏板已满,否则为false。
*/
公共布尔值isFull()
{
对于(int i=0;i<3;i++)
对于(int j=0;j<3;j++)
if(单元格[i][j].getToken()='')
返回false;
返回true;
}
/**
*确定给定令牌是否已获胜。
*@param token测试是否获胜
*@return True,如果令牌赢了,否则为false。
*/
公共布尔isWon(字符标记)
{
//检查行
对于(int i=0;i<3;i++)
if((单元格[i][0].getToken()==标记)
&&(单元格[i][1]。getToken()==标记)
&&(单元格[i][2].getToken()==令牌)
{
返回true;
}
//检查列
对于(int j=0;j<3;j++)
if((单元格[0][j].getToken()==标记)
&&(单元格[1][j].getToken()==标记)
&&(单元格[2][j].getToken()==标记)
{
返回true;
}
//检查对角线
if((单元格[0][0].getToken()==标记)
&&(单元格[1][1]。getToken()==标记)
&&(单元格[2][2]。getToken()==标记))
{
返回true;
}
if((单元格[0][2].getToken()==标记)
&&(单元格[1][1]。getToken()==标记)
&&(单元格[2][0]。getToken()==标记))
{
返回true;
}
返回false;
}
/**
*定义TictaToe游戏板中的单元格。
*/
公共类单元扩展了JPanel
{
/**
* 
*/
私有静态最终长serialVersionUID=1L;
//此单元格的标记
私有字符令牌=“”;
/**
*建造师
*/
公共单元格()
{
设置顺序(新的线边框(颜色为黑色,1));
addMouseListener(新的MyMouseListener());
}
/**
*获取单元格的标记。
*@返回单元格的标记值。
*/
公共字符getToken()
{
返回令牌;
}
/**
*设置单元格的标记。
*@param c字符用作令牌值。
*/
公共void setToken(字符c)
{
令牌=c;
重新油漆();
}
@凌驾
受保护组件(图形g)
{
超级组件(g);
如果(标记='X')
{
g、 抽绳(10,10,getWidth()-10,getHeight()-10);
g、 抽绳(getWidth()-10,10,10,getHeight()-10);
}
else if(标记='O')
{
g、 drawOval(10,10,getWidth()-20,getHeight()-20);
}
}
私有类MyMouseListener扩展了MouseAdapter
{
@凌驾
公共无效mouseClicked(MouseEvent e)
{
如果(游戏结束)
返回;
//如果手机是空的,游戏还没有结束
如果(令牌=''&&whoseTurn!='')
设置令牌(whoseTurn);
//检查游戏状态
如果(isWon(whoseTurn))
{
jlbstatus.setText(whoseTurn+“赢了!游戏结束!”);
whoseTurn='';
gameOver=true;
}
else if(isFull())
{
setText(“平局!游戏结束!”);
whoseTurn='';
gameOver=true;
}
其他的
{
whoseTurn=(whoseTurn='X')?'O':'X';
jlbstatus.setText(whoseTurn+“轮到你了”);
}
}
}//结束类MyMouseListener
}//结束类单元格
}//结束类TicTacToeFrame

JFrame-ticTacToe=TicTacToeFrame()应该是
JFrame ticTacToe=new TicTacToeFrame()


按原样,您正在调用一个返回
null
的本地方法——之后的第一行将失败,因为它试图针对
null
调用方法应该是
JFrame ticTacToe=new
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
private static JFrame TicTacToeFrame() 
{
    // method for TicTacToeFrame
    return null;
}
    JFrame ticTacToe = new TicTacToeFrame();
private static JFrame TicTacToeFrame() 
{
    // method for TicTacToeFrame
    return null;
}
public TicTacToeFrame()
{
    ....
}
JFrame ticTacToe = new TicTacToeFrame();