0

OK, so I'm creating a Tic Tac Toe GUI. I currently having a working frame of 3x3 buttons that will change to either an X or O. My issue is implementing a JLabel on the top of the buttons. The label keeps track of who's turn it is and will change from either "O's turn" or "X's turn" however I can't seem to get the JLabel to update (only spouts errors). Here's a portion of my code showing how Trying to implement this. The program runs fine but as soon as I add the (turn.setText("O's turn");) I get errors.

Any feedback as to why this may not be working would be appreciated.

import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class TicTacToe extends JFrame { XOButton[] xob = new XOButton[9]; private JLabel turn; public static int state; public TicTacToe() { super("TicTacToe"); //add XOButtons to panel JPanel center = new JPanel(); state = 0; JLabel turn = new JLabel("X's turn"); center.setLayout(new GridLayout(3, 3, 2, 2)); //action listener ButtonListener bl = new ButtonListener(); for (int i = 0; i < 9; i++) { xob[i] = new XOButton(); xob[i].addActionListener(bl); center.add(xob[i]); } //add panel to frame setLayout(new BorderLayout()); add(center, BorderLayout.CENTER); add(turn, BorderLayout.NORTH); } //inner action listener class private class ButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent ae) { for (int i = 0; i < 9; i++) { if(ae.getSource() == xob[i] && xob[i].getState() == XOButton.NONE && state == 0) { xob[i].setState(XOButton.X); state = 1; //turn.setText("O's turn"); } else if(ae.getSource() == xob[i] && xob[i].getState() == XOButton.NONE && state == 1) { xob[i].setState(XOButton.O); state = 0; } } } } } 
2
  • 1
    "... I get errors" -- and you'll want to show us those errors as well as indicate which line causes it. Otherwise we'll have a hard time understanding what your problem may be. Commented Apr 27, 2015 at 23:57
  • Please see edits to answer. You're shadowing the turn variable and the solution is not to do this. Commented Apr 28, 2015 at 0:01

1 Answer 1

4

Guess: your error is a NullPointerException because turn is null. Solution: don't shadow the variable. In other words you're re-declaring the turn variable in the constructor leaving the field null.

e.g.,

public class TicTacToe extends JFrame{ XOButton[] xob = new XOButton[9]; private JLabel turn; public static int state; public TicTacToe() { super("TicTacToe"); //add XOButtons to panel JPanel center = new JPanel(); state = 0; JLabel turn = new JLabel("X's turn"); // **** you're re-declaring turn here! 

Better:

public class TicTacToe extends JFrame{ XOButton[] xob = new XOButton[9]; private JLabel turn; public static int state; public TicTacToe() { super("TicTacToe"); //add XOButtons to panel JPanel center = new JPanel(); state = 0; turn = new JLabel("X's turn"); // **** Note the difference? 

In the future, if you have similar problems, please post the entire error message and indicate which line throws the exception as it will help us immensely!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, works perfectly now. I didn't add the error only because it was about as long as my code but I'll remember to be more descriptive in the future.
@Devos: don't worry about the error being long -- add it in code tags, meaning add it to your question, highlight it, and then press the code button so that it gets indented and is presented as code. This way we can scroll through as much as we like. But also take care to indicate by some obvious means, perhaps an obvious comment // ******* here !!!! **** where the error occurs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.