我一直在一个项目上工作,该项目是创建一个零和十字架的游戏。我已经为这款游戏奠定了基础,目前正在进一步开发。但是,当我运行该程序时,选择AI进行第一步操作,返回以下代码行中的Java Null指针异常:
if(Game.Board[0][0].getText().equals(Game.Board[1][1].getText()) && Game.Board[0][0].getText().equals(Game.PlayerMark))Game.Boarda由3x3的Jbutton组成。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方法(它的片段,否则会显示太多):
public static void AI() {
for(int i=0; i<3; i++ )
{
for (int j=0; j<3; j++)
{
// Diagonal Defensive Strategy
if(Game.Board[0][0].getText().equals(Game.Board[1][1].getText()) && Game.Board[0][0].getText().equals(Game.PlayerMark))
{
if( !Game.Board[2][2].getText().equals(Game.ComputerMark) && !Game.Board[2][2].getText().equals(Game.PlayerMark))
{
Game.Board[2][2].setText(Game.ComputerMark);
MadeMove = true;
return;
}
}
if(Game.Board[2][2].getText().equals(Game.Board[1][1].getText()) && Game.Board[2][2].equals(Game.PlayerMark))
{
if( Game.Board[0][0].getText().equals(Game.ComputerMark) && !Game.Board[0][0].getText().equals(Game.PlayerMark))
{
Game.Board[0][0].setText(Game.ComputerMark);
MadeMove = true;
return;
}
}
if(Game.Board[0][0].getText().equals(Game.Board[1][1].getText()) && Game.Board[0][0].getText().equals(Game.PlayerMark))
{
if( !Game.Board[2][2].getText().equals(Game.ComputerMark) && !Game.Board[2][2].getText().equals(Game.PlayerMark))
{
Game.Board[2][2].setText(Game.ComputerMark);
MadeMove = true;
return;
}
}发布于 2015-03-03 01:12:49
下面是如何解决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];等-我不确定是什么类型,但你会知道的。
再运行一次,看看它掉在哪里了。行号现在将准确地告诉您哪条语句失败了。该语句中的引用必须为空。
如果您不知道引用为空的原因,请按照逻辑返回并重复上面的更改。
您还可以学习在诸如Eclipse之类的IDE中使用调试器。
https://stackoverflow.com/questions/28814800
复制相似问题