我正在开发一个tic tac toe游戏并且遇到一个问题 - 线程异常" AWT-EventQueue-0" java.lang.NullPointerException,当我尝试按下按钮时发生。 buttonText更改为X,但之后我遇到了问题。我认为if语句有问题: Main.java的源代码:
package mytictactoegame;
public class Main {
public static boolean playerTurn = true;
public static boolean playerWon = false;
public static boolean compWon = false;
public static boolean playing = true;
public static ticgame board = new ticgame ();
public static void main(String[] args) {
board.setVisible(true);
}
public static void checkforwin (){
//147
if (board.button1.getText().equals("X")){
if (board.button4.getText().equals("X")){
if (board.button7.getText().equals("X")){
playerWon = true;
compWon = false;
board.labelWon.setText ("Player X won!");
}
}
}
}
}
ticgame(GUI):
package mytictactoegame;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.GridLayout;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.RowSpec;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JLabel;
public class ticgame extends JFrame {
Main main = new Main ();
/**
*
*/
public static final long serialVersionUID = 1L;
public JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ticgame frame = new ticgame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ticgame() {
setTitle("Tic Tac Toe");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBackground(new Color(204, 255, 0));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new GridLayout(3, 3, 0, 0));
JButton button1 = new JButton("");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (button1.getText().equals("")){
if (Main.playerTurn == true){
button1.setText("X");
Main.checkforwin();
Main.playerTurn = false;
} else {
button1.setText("O");
Main.checkforwin();
Main.playerTurn = true;
}
}
}
});
panel.add(button1);
JButton button4 = new JButton("");
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (button4.getText().equals("")){
if (Main.playerTurn == true){
button4.setText("X");
Main.checkforwin();
Main.playerTurn = false;
} else {
button4.setText("O");
Main.checkforwin();
Main.playerTurn = true;
}
}
}
});
panel.add(button4);
JButton button7 = new JButton("");
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (button7.getText().equals("")){
if (Main.playerTurn == true){
button7.setText("X");
Main.checkforwin();
Main.playerTurn = false;
} else {
button7.setText("O");
Main.checkforwin();
Main.playerTurn = true;
}
}
}
});
panel.add(button7);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.EAST);
panel_1.setLayout(new FormLayout(new ColumnSpec[] {
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
ColumnSpec.decode("117px"),},
new RowSpec[] {
FormFactory.RELATED_GAP_ROWSPEC,
RowSpec.decode("29px"),
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,}));
JButton buttonNewGame = new JButton("New Game");
buttonNewGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button1.setText("");
button2.setText("");
button3.setText("");
button4.setText("");
button5.setText("");
button6.setText("");
button7.setText("");
button8.setText("");
button9.setText("");
Main.playerTurn = true;
Main.playerWon = false;
Main.compWon = false;
}
});
buttonNewGame.setForeground(Color.RED);
buttonNewGame.setBackground(new Color(153, 255, 0));
panel_1.add(buttonNewGame, "2, 2, left, top");
JButton buttonHistory = new JButton("History");
buttonHistory.setForeground(Color.RED);
panel_1.add(buttonHistory, "2, 4");
JButton buttonExit = new JButton("Exit");
buttonExit.setForeground(Color.RED);
buttonExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
panel_1.add(buttonExit, "2, 6");
JLabel labelWon = new JLabel();
labelWon.setText ("Who won?");
labelWon.setForeground(Color.GREEN);
panel_1.add(labelWon, "2, 10, left, default");
}
public JButton button1;
public JButton button2;
public JButton button3;
public JButton button4;
public JButton button5;
public JButton button6;
public JButton button7;
public JButton button8;
public JButton button9;
public JLabel labelWon;
}
这是错误日志的第一行,其余部分由于单词数量而不合适:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at mytictactoegame.Main.checkforwin(Main.java:20)
at mytictactoegame.ticgame$2.actionPerformed(ticgame.java:71)
答案 0 :(得分:1)
您正在隐藏变量,例如button1变量和其他类似变量。通过阴影,我的意思是你在类中声明它们,然后重新声明并在类的构造函数中初始化重新声明的变量。执行此操作时,该字段(在类中声明的变量)保持为null。解决方案:不要通过不在构造函数中重新声明变量来影响变量。
例如,你这样做:// should be named TicGame to comply with Java naming standards
public class ticgame extends JFrame {
public ticgame() {
// ....
// here you re-declare the button1 variable
// by doing this, you initialize the local variable that
// is present int he constructor but leave the class field null
JButton button1 = new JButton("");
//....
}
public JButton button1; // this guy remains null
// .....
}
当你应该这样做时:
public class TicGame extends JFrame {
public TicGame() {
// ....
// JButton button1 = new JButton("");
button1 = new JButton(""); // note the difference?
//....
}
public JButton button1; // now he's not null!
// .....
}