问题描述:

I have been working on a project and the project is to create a game of Noughts and Crosses. I have already established the groundwork for the game and I'm currently developing it further. However , when I run the program and the AI is chosen to make the first move , I was returned a Java Null pointer exception in this line :

if(Game.Board[0][0].getText().equals(Game.Board[1][1].getText()) && Game.Board[0][0].getText().equals(Game.PlayerMark))

Game.Board[a][b] consists of 3x3 Jbuttons. PlayerMark is a string that either can contain "X" or "O".

How to solve this problem?

Where AI method is being called:

 public void StartGame()

{

SideAssigner();

State = true;

if ( AIGame == true && FirstTurn ==true)

{

Computer.AI();

}

while ( State = true )

{

WinValidator();

}

}

Message in console box :

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

Update :

Here is an example of how each JBUtton's properties are determined:

 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 Method ( snippet of it otherwise it would be too much to display) :

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;

}

}

网友答案:

Here is how to solve a NullPointerException. You should learn how to solve these problems yourself as you will get a lot of them:

Look at your stack trace:

at Computer.AI(Computer.java:19)
at Game.StartGame(Game.java:179)

Find the line at the top of the trace:

if(Game.Board[0][0].getText().equals(Game.Board[1][1].getText()) && Game.Board[0][0].getText().equals(Game.PlayerMark))

If you can't see the error, split the line up into smaller statements:

tmp = Game.Board[0];
tmp2 = tmp1[0];
tmp3 = Game.Board[1];
tmp4 = tmp3[1];

etc, etc - I am not sure what the types are, but you will know that.

Run it again and see where it falls over. The line number will now tell you exactly which statement has failed. The reference in that statement must be null.

If you don't know why the reference is null, follow the logic back and repeat the changes above.

You could also learn to use the debugger in an IDE such as Eclipse.

相关阅读:
Top