0

I have the following constructor which defines a board and checks if any of the three JButtons have been clicked:

Timer timer = new Timer(500, this);

private boolean[][] board;
private boolean isActive = true;
private int height;
private int width;
private int multiplier = 40;

JButton button1;
JButton button2;
JButton button3;
public Board(boolean[][] board) {
    this.board = board;
    height = board.length;
    width = board[0].length;
    setBackground(Color.black);
    button1 = new JButton("Stop");
    add(button1);
    button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            isActive = !isActive;
            button1.setText(isActive ? "Stop" : "Start");
        }
    });
    button2 = new JButton("Random");
    add(button2);
    button2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            this.board = randomBoard();
        }
    });
    button3 = new JButton("Clear");
    add(button3);
    button3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            this.board = clearBoard();
        }
    });
}

But it returns this error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    board cannot be resolved or is not a field
    board cannot be resolved or is not a field

Why is this? How do I access this.board in the constructor?

1
  • Where have you defined board? Commented May 14, 2013 at 15:36

4 Answers 4

4

The problem is caused by you trying to access this.board inside the anonymous inner classes. Since there is no board field defined, that causes an error.

For example this:

button2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        this.board = randomBoard();
    }
});

In order to be able to use the board variable inside your anonymous inner classes you either need to remove this or use something like Board.this.board (if you want to be more explicit).

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

6 Comments

From a non-static inner class (even an anonymous one) you can just access the member variables of the outer class, as long as you remove this (so just use board = randomBoard();)
@Heuster: This only works when the board parameter would be final. But then he could not assign a value to it.
@mszalbach I don't think fields need to be final. final is only for the case of local variables so that they are not popped out of stack when the method exists.
@Cemre: Its not the field which needs to be final but the parameter passed into the constructor. Because this is what you want to access when you just remove the this before board. To access the field of Board it only helps to use Board.this.board.
@mszalbach For the case of passing as a parameter, yes you probably need to make it final. But in this case it works to just remove this since there is no other board variable the anonymous inner class defines. You dont have to use Board.this.board. I just tested to make sure.
|
0

This should work:

Board.this.board = randomBoard();

The problem is this related to the class ActionListener which did not have a board variable. However with Board.this you specify that you mean the board member of the Board class. This is the syntax you need to use for nested classes to access the variables of the outer class.

Comments

0

The problem is here

button2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        this.board = randomBoard();
    }
});

your button2 is a JButton, and it doesn't have a board field. Don't access it with this.board. Find another way to access board.


The fast and dirty way to do it would be to make board static, but I'm sure that JButton has a way for you to specify a parent(your board class), and access the board field that way.

After a quick look at The documentation for JButton, I've found a getParent method. I'd start with that.

Comments

-1

As others and the compiler said, you don't have a field (which is an instance member), but only a local variable board, which goes out of scope as soon as constructor exits.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.