零和交叉项目中的java空指针异常
我一直在做一个项目,这个项目是创造一个零和十字架的游戏。我已经为游戏打下了基础,现在我正在进一步发展它。但是,当我运行程序并选择AI进行第一步操作时,我在这一行返回了一个Java空指针异常:零和交叉项目中的java空指针异常,java,eclipse,oop,tic-tac-toe,Java,Eclipse,Oop,Tic Tac Toe,我一直在做一个项目,这个项目是创造一个零和十字架的游戏。我已经为游戏打下了基础,现在我正在进一步发展它。但是,当我运行程序并选择AI进行第一步操作时,我在这一行返回了一个Java空指针异常: if(Game.Board[0][0].getText().equals(Game.Board[1][1].getText()) && Game.Board[0][0].getText().equals(Game.PlayerMark)) 游戏板[a][b]由3x3个按钮组成。Player
if(Game.Board[0][0].getText().equals(Game.Board[1][1].getText()) && Game.Board[0][0].getText().equals(Game.PlayerMark))
游戏板[a][b]由3x3个按钮组成。PlayerMark是一个可以包含“X”或“O”的字符串
如何解决这个问题
在调用AI方法的地方:
public void StartGame()
{
SideAssigner();
State = true;
if ( AIGame == true && FirstTurn ==true)
{
Computer.AI();
}
while ( State = true )
{
WinValidator();
}
}
控制台框中的消息:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Computer.AI(Computer.java:19)
at Game.StartGame(Game.java:179)
at Game$10.actionPerformed(Game.java:739)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Sour
更新:
下面是如何确定每个JBUtton属性的示例:
Button1.setText("");
Button1.setEnabled(false);
Button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if ( State == true && playerO == true && PlayerGame == true )
{
isEmpty = false;
Button1.setText("O");
// WinValidator
// TurnChecker ??
}
else if ( State == true && playerX == true && PlayerGame == true )
{
isEmpty = false;
Button1.setText("X");
// WinValidator
// TurnChecker
}
else if ( State == true && computerO == true && AIGame == true)
{
isEmpty = false;
// Call Computer Class
// WinValidator
// TurnChecker
}
else if ( State == true && computerX == true && AIGame == true)
{
isEmpty = false;
// Call Computer Class
// WinValidator
// TurnChecker
}
}
});
AI方法(它的片段,否则将太多而无法显示):
公共静态无效AI()
{
for(int i=0;i以下是如何解决NullPointerException
。您应该自己学习如何解决这些问题,因为您会遇到很多问题:
查看堆栈跟踪:
at Computer.AI(Computer.java:19)
at Game.StartGame(Game.java:179)
在跟踪的顶部找到该行:
if(Game.Board[0][0].getText().equals(Game.Board[1][1].getText()) && Game.Board[0][0].getText().equals(Game.PlayerMark))
如果看不到错误,请将行拆分为较小的语句:
tmp = Game.Board[0];
tmp2 = tmp1[0];
tmp3 = Game.Board[1];
tmp4 = tmp3[1];
等等,等等-我不确定是什么类型,但你会知道的
再次运行它,看看它落在哪里。行号现在将准确地告诉您哪个语句失败了。该语句中的引用必须为null
如果您不知道为什么引用为null,请按照逻辑返回并重复上面的更改
您还可以学习在Eclipse等IDE中使用调试器。Game.Board[0][0]或Game.Board[1][1]是空的。如何解决它,我不知道,我想我们需要更多的源代码。@RobNeal编辑您的问题,不要将其作为注释发布,甚至更糟糕的是作为答案发布。发布您的AI方法以尽量避免使用大写名调用变量/实例(StartGame
,MadeMove
等).虽然有效,但它违反惯例,使阅读变得困难。
tmp = Game.Board[0];
tmp2 = tmp1[0];
tmp3 = Game.Board[1];
tmp4 = tmp3[1];