线程中的异常“AWT-EventQueue-0” java.lang.NullPointerException if-statement [英] Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException if-statement

查看:2486
本文介绍了线程中的异常“AWT-EventQueue-0” java.lang.NullPointerException if-statement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个tic tac toe游戏并且有一个问题 - 线程AWT-EventQueue-0中的异常java.lang.NullPointerException,它在我尝试按下按钮时发生。 buttonText更改为X,但之后我遇到了问题。我认为if语句有问题:
Main.java的源代码:

I'm developing a tic tac toe game and have one problem - Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException, it occurs when i try to press the button. The buttonText is changed to X, but after that i get the problem. I believe there is something wrong with if statements: The source code of 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):

And 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;

}

这是第一行错误日志,其余部分由于单词数量不合适:

Here is first lines of the error log, the rest doesnt fit due to number of words:

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变量和其他类似变量。通过阴影,我的意思是你在类中声明它们,然后重新声明并在类的构造函数中初始化重新声明的变量。当您这样做时,该字段(在类中声明的变量)保持为null。解决方案:不要通过不在构造函数中重新声明变量来影响变量。

You're shadowing your variables such as your button1 variable and others like it. By shadowing, I mean that you declare them in the class, but then re-declare and initialize the re-declared variable in your class's constructor. When you do that, the field (the variable declared in the class) remains null. Solution: don't shadow your variables by not re-declaring them in the constructor.

例如,你这样做:

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

这篇关于线程中的异常“AWT-EventQueue-0” java.lang.NullPointerException if-statement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆