0

I'm quite new to coding so please be kind!

I'm trying to code a tic tac toe game in Java Swing. But I'm having a quite annoying error and I don't understand why this error is being displayed. Can anyone help me understand why please?

this is my code :

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Board extends JFrame {
    private JPanel pan = new JPanel();
    private  boolean player1 = true;
    private JButton[] tab_but = new JButton[9];

    public Board() {
        initializeComponent();          
    }
    public void initializeComponent() {
        this.setTitle("TicTacToe");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(400, 400);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setContentPane(pan);
        GridLayout gl = new GridLayout(3,3);
        pan.setLayout(gl);
        for (JButton jButton : tab_but) {
            jButton = new JButton();
            pan.add(jButton);
            jButton.addActionListener(new ButtonListener());
        }
        this.setVisible(true);      
    }
    class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {

            if (player1) {
                ((JButton)e.getSource()).setText("X");
                checkWin();
                player1=false;  
            }
            else {
                ((JButton)e.getSource()).setText("O");
                player1=true;
            }   
        }   
    }
    public void checkWin() {
        if(tab_but[0].getText().equals("X")) {
            System.out.println("hi");
        }
    }
}

As you can see I'm trying to check with the method checkWin() if the text of the first button is an "X". But the console returns java.lang.NullPointerException even though the X has been written on the first button (when I clicked on the first button)

Frakcool
  • 10,915
  • 9
  • 50
  • 89
Ruchiha
  • 69
  • 7
  • `if(tab_but[0].getText().equals("X"))`. In your code you initialize the array, but not every element of `tab_but` and thus it's null – Frakcool Jun 13 '18 at 18:00
  • What's the full stack trace? – ifly6 Jun 13 '18 at 18:01
  • 1
    I'd indeed consider this as a duplicate. You'll have to learn to figure out the reason for a `NullPointerException` in general. Think about which variable can be `null` there, and where it should be initialized to a non-null value. In this case, change `for (JButton jButton : tab_but) {` to `for (int i=0; i<9; i++) { JButton jButton = new JButton(); tab_but[i] = jButton; ... ...` – Marco13 Jun 13 '18 at 18:03
  • @Frakcool aren't every element initialized in the foreach loop by this line : `jButton = new JButton();` ? @Marco13 even when I create all the buttons manually `JButton button1 = new JButton();` until the 9th and that I try to access the text written in it after I had set it to X, it returns a nullpointerexception – Ruchiha Jun 14 '18 at 09:51
  • I've solved it. Thanks everyone. – Ruchiha Jun 14 '18 at 13:31
  • As @Marco13 said, I'm not sure why but initializing buttons in a `for-each` doesn't work, while using a normal `for-loop` does. – Frakcool Jun 14 '18 at 17:06

1 Answers1

0

What line is throwing a NullPointerException, the one with the .equals("X") or the .setText("X")? I believe it's because you initialize the array tab_but but do not fill it with objects, therefore tab_but[0] is null.