Can't access variable - Newbie - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Can't access variable - Newbie

alice35alice35 Posts: 1Member
Hi everybody,
I'm quite the newbie to Java and this is my first post on this forum.
The question i have is actually for a homework project (a kind of PacMan game).
the code creates a button array of rows*columns buttons that should each have an action listener attached. by clicking a button, a block should be placed in a grid. My problem is that i can't access the value (row, column) of the button within putBlockListener.

This is my code, and i receive an error at line 58 where it says
TwoWindowsMarc.aGrid.block(MonsterBlocks.row, MonsterBlocks.col);
cannot find symbol

package FirstMonster;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;

public class MonsterBlocks extends JFrame {

private MonsterBlocks(int row, int col)
JFrame buttonFrame = new JFrame("build Buttons");
buttonFrame.setLayout(new GridLayout(row,col, 1, 1));

JButton[][] blockButton = new JButton[row][col];

int r = 0;
int c = 0;
while (r < row)
while (c < col)
if(TwoWindowsMarc.aGrid.rectangle[r][c] == TwoWindowsMarc.aGrid.blockChar)
blockButton[r][c]=new JButton("#");
else if(TwoWindowsMarc.aGrid.rectangle[r][c] == TwoWindowsMarc.aGrid.thingHereChar)
blockButton[r][c]=new JButton("O");
blockButton[r][c]=new JButton(".");

blockButton[r][c].setFont(new Font("Sans Serif", 1, 8));
blockButton[r][c].setMargin(new Insets(1, 1, 1, 1));

putBlockListener putBlocksListen= new putBlockListener( );
c = 0;

class putBlockListener implements ActionListener
// when the Build Grid button is clicked to this...
public void actionPerformed(ActionEvent e )
TwoWindowsMarc.aGrid.block(MonsterBlocks.row, MonsterBlocks.col); //this is were i get the error
TwoWindowsMarc.aGrid.updateState( );

} //end of gridListener


Any help would be greatly appreciated!



  • silveredge52silveredge52 Posts: 198Member
    Only partial code is here so definitive answer is questionable. However it appears that what is "missing" are MonsterBlocks class variables, ie. symbols, [u]row[/u] and [u]int[/u].

    Hope this helps,
Sign In or Register to comment.