gpt4 book ai didi

java - 线程 "AWT-EventQueue-0"java.lang.NullPointerException if 语句中的异常

转载 作者:行者123 更新时间:2023-11-30 11:05:16 25 4
gpt4 key购买 nike

我正在开发一款井字游戏,遇到一个问题 - 线程“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)

最佳答案

您正在隐藏您的变量,例如您的 button1 变量和其他类似的变量。通过隐藏,我的意思是您在类中声明它们,然后在类的构造函数中重新声明 并初始化重新声明的变量。当您这样做时,该字段(在类中声明的变量)保持为空。解决方案:不要通过不在构造函数中重新声明变量来隐藏您的变量。

例如,您这样做:

// 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!
// .....
}

关于java - 线程 "AWT-EventQueue-0"java.lang.NullPointerException if 语句中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29709633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com