0

So I'm creating a library management program using jframes in netbeans, I know it might not be the best solution but it's what I'm required to use in my course. Basically, every button action is controlled from one central private method, after a couple some username and password verification, I successfully create an object of the Librarian class called currentLibrarian. Until then I get no error, but when I try to reference this object outside the method it can't be found. the code is pretty extensive, and I understand there may be some inefficiencies, but I'm just trying to focus on this problem right now. The following is a snippet of relevant code there is some past that but it isn't relevant:

    private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        if(usernameField.getText().isEmpty()||passwordField.getText().isEmpty()){
            JOptionPane.showMessageDialog(null, "Please Appropriately Fill all Fields");
        }
        else{
            String inputUsername = usernameField.getText();
            int inputPassword = Integer.parseInt(passwordField.getText());
            ConnectionDB connect= new ConnectionDB();
            try {
                Statement stm = (connect.connectDB()).createStatement();
                String query = "select * from library.username_password where username = '"+inputUsername+"';";
                ResultSet usernamePasswordResultSet = stm.executeQuery(query);
                /**
                 * When an invalid username is requested from mySQL database, a 'false' boolean value is returned in Result Set
                 * Thus if the username input is invalid the result of userInfoResultSet.next() should be false
                 * furthermore .next() moves cursor to start position of the result set and allows us to begin reading values
                 */
                if(!usernamePasswordResultSet.next()){
                    failedAttempt();
                    JOptionPane.showMessageDialog(null, "Username is Invalid");
                }
                else{
                    /**if the username is valid we can retreive the stored password at that location
                     * then verify password validity
                     */
                    int databasePassword = usernamePasswordResultSet.getInt("password");
                    if(databasePassword != inputPassword){
                        failedAttempt();
                        JOptionPane.showMessageDialog(null, "Password is Invalid");                        
                    }
                    /**
                     * if both username and password are valid we need to verify what type of user we have
                     * then redirect them to the respective main menu
                     */ 
                    else{
                        boolean isLibrarian = usernamePasswordResultSet.getBoolean("isLibrarian");
                        if(isLibrarian){
                            Librarian currentLibrarian = new Librarian(usernamePasswordResultSet.getBoolean("isHeadLibrarian"));
                            this.setVisible(false);
                            new LibrarianMainMenuForm().setVisible(true);
                        }

The following code is where I try to reference it from:

  private void libraryStatsActionPerformed(java.awt.event.ActionEvent evt) {                                             
        if(currentLibrarian.getIsHeadLibrarian()){
        }
    }                     

I was hoping that in the second section of code, I'd get no error instead i'm told that I 'cannot find symbol'

  • You look to have a problem of variable ***scope***. You have declared and initialized the variable like so: `Librarian currentLibrarian = new Librarian(...)` *inside* of an if block that is inside of a method, and so that variable is only visible within the same if block. If you need it throughout the method, then declare it in method scope. If you need it throughout the instance, than declare the variable as a private instance variable. If you need it in other objects, then perhaps give it a getter method. – Hovercraft Full Of Eels Apr 16 '23 at 14:28

0 Answers0