我有一个按钮,我希望它在单击时运行一个方法,这是该按钮的代码...

             playAgainButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        action(e);
                    }

public void action(ActionEvent e) {

                        Main.playerTurn = true;
                        Main.computerWon = false;
                        Main.playerWon = false;

                        button1.setText("");
                        button2.setText("");
                        button3.setText("");
                        button4.setText("");
                        button5.setText("");
                        button6.setText("");
                        button7.setText("");
                        button8.setText("");
                        button9.setText("");
                    }
                    });


但是我在这里也收到了NullPointer错误...

java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
    at tictactoe.tictactoe.initComponents(tictactoe.java:281)
    at tictactoe.tictactoe.<init>(tictactoe.java:13)
    at tictactoe.Main.<clinit>(Main.java:30)
Exception in thread "main" Java Result: 1

最佳答案

playAgainButton可能是null!检查是否为null

System.out.print("Is playAgainButton null? " + (playAgainButton == null));

关于java - NullPointerException关于Java中的 Action 声明,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13060318/

10-09 18:35