0

I am getting an error that says "cannot refer to a non-final variable inside an inner class defined in a different method"

This is happening when I am trying to set up my timer referencing an action listener that I have constructed! This constructor simply displays a label and the timers desired effect is to run the action listener for 10 seconds but I can't get past this error. The code below shows he action listener and where he issue is occurring. I have put stars around the problematic areas. Thanks

**ActionListener actListner = new ActionListener() {                         
        public void actionPerformed(ActionEvent event) {        
            lblgreenImage.setBounds(150,410,50,40);
            repaint();          
        }       
    };**


    btnEnter.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent ae) 
        {

            String username = txtUserName.getText();
            String accessCode = txtPin.getText();


            String dataSourceName = "securitysystem";
            String dbUrl = "jdbc:odbc:" + dataSourceName;

            try{
                //Type of connection driver used    
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                //Connection variable or object param: dbPath, userName, password
                Connection con = DriverManager.getConnection(dbUrl, "", "");

                Statement statement = con.createStatement();

                ResultSet rs = statement.executeQuery("select username, pin from securitysystem.employee where username = '" + username + "' and pin = '" + accessCode + "'");

                boolean nomatches=true;
                while(rs.next())
                {   
                    String checkrs = rs.getString("username");
                    String checkPin = rs.getString("Pin");
                    if (username.equals(checkrs)) {
                        if (accessCode.equals(checkPin)) {
                            **Timer timer = new Timer(500, actListner);**
                            **timer.start();**

1 Answers1

0

Seems you try to access local variable lblgreenImage in AbstractClass. For fixing that you can:

1) add final modifier for lblgreenImage, when you create that, like next :

final JLabel lblgreenImage = new JLabel("labeel");

2) make lblgreenImage as instance variable.

Read more about variables in java.

alex2410
  • 10,904
  • 3
  • 25
  • 41