Search code examples
javaswingcompiler-errorsjlabel

Reset Java field unresolved compilation problem


I'm trying to count the number of tries a user makes to guess a number in a java GUI program using swing.

I've created these variables to keep track of the number of tries:

    JLabel lblNumberOfTries = new JLabel("Number of tries:");
    lblNumberOfTries.setBounds(136, 219, 90, 13);
    getContentPane().add(lblNumberOfTries);
    
    txtNumberOfTries = new JTextField();
    txtNumberOfTries.setBounds(223, 219, 42, 13);
    getContentPane().add(txtNumberOfTries);
    txtNumberOfTries.setColumns(10);

And I need to reset them when the game restarts. For some reason I can reset the java field but not the label:

txtNumberOfTries.setText(""); // this is ok
lblNumberOfTries.setText(""); // this gives an error

This is the error I'm getting:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    lblNumberOfTries cannot be resolved

    at GuessingGame.newGame(GuessingGame.java:50)
    at GuessingGame.main(GuessingGame.java:110)

This is the class and functions where the labels and fields are in my program:

import javax.swing.JFrame;      
    
    public class GuessingGame extends JFrame {
        public GuessingGame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Tim's Hi-Lo Guessing Game");
        getContentPane().setLayout(null);

        JLabel lblTitle = new JLabel("Tim's Hi-Lo Guessing Game");
        lblTitle.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
        lblTitle.setBounds(0, 10, 436, 32);
        getContentPane().add(lblTitle);

        JLabel lblGuessANumber = new JLabel("Guess a number between 1 and 100:");
        lblGuessANumber.setBackground(new Color(240, 240, 240));
        lblGuessANumber.setHorizontalAlignment(SwingConstants.RIGHT);
        lblGuessANumber.setBounds(147, 54, 215, 13);
        getContentPane().add(lblGuessANumber);

        txtGuess = new JTextField();
        txtGuess.addActionListener((ActionEvent e) -> {
            checkGuess();
        });
        txtGuess.setHorizontalAlignment(SwingConstants.RIGHT);
        txtGuess.setBounds(260, 122, 27, 19);
        getContentPane().add(txtGuess);
        txtGuess.setColumns(10);

        JButton btnGuess = new JButton("Guess!");
        btnGuess.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                checkGuess();
            }
        });
        btnGuess.setBounds(172, 121, 85, 21);
        getContentPane().add(btnGuess);

        lblOutput = new JLabel("Enter a number above and click Guess!");
        lblOutput.setHorizontalAlignment(SwingConstants.CENTER);
        lblOutput.setBounds(58, 196, 350, 13);
        getContentPane().add(lblOutput);
        
        JLabel lblNumberOfTries = new JLabel("Number of tries:");
        lblNumberOfTries.setBounds(136, 219, 90, 13);
        getContentPane().add(lblNumberOfTries);
        
        txtNumberOfTries = new JTextField();
        txtNumberOfTries.setBounds(223, 219, 42, 13);
        getContentPane().add(txtNumberOfTries);
        txtNumberOfTries.setColumns(10);
}

public void newGame() {
    theNumber = (int) (Math.random() * 100 + 1);
    txtNumberOfTries.setText("");
    lblNumberOfTries.setText("");
    
}

}

What am I doing wrong?


Solution

  • Ok, so after seeing the full file, there are 2 main points:

    You need to define a field private JLabel lblNumberOfTries; in the class

    You also need to remove 'JLabel' from around line 98 in the GuessingGame constructor (the method defined public GuessingGame() is known as the constuctor) so that it becommes lblNumberOfTries = new JLabel("Number of tries:");. This will stop you from redeclaring the variable.

    At some point, you're also going to want to add a private int field called numberOfTries