Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException if-statement

大兔子大兔子 提交于 2019-12-11 02:01:40

问题


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!");
            }
        }
    }
}
}

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)

回答1:


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.

e.g., you do this:

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

When you should be doing this:

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


来源:https://stackoverflow.com/questions/29709633/exception-in-thread-awt-eventqueue-0-java-lang-nullpointerexception-if-stateme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!