How to fix an empty variable

77 Views Asked by At

I am making a password vault for a school project and I am trying to compare the "masterPassword" with the "enteredPassword" but whenever I test the "enteredPassword" it returns as a null variable. How do I fix that?

I have tried commenting out the code actionListener and having it simply showConfirmDialog(null, enteredPassword) and it returns blank.

import java.awt.*;
import java.awt.event.*;

public class PasswordVault implements ActionListener
{
    static String masterPassword;
    static String enteredPassword;

    public static void  main(String[] args) 
    {   
        String name = JOptionPane.showInputDialog("What is your name?");
        masterPassword = JOptionPane.showInputDialog("Hello, " + name + ". " + "What would you like your master password to be?");

        new PasswordVault();
    }

    public PasswordVault()
    {
        JFrame masterPasswordFrame = new JFrame("Master Password");
        masterPasswordFrame.setSize(400,100);
        masterPasswordFrame.setLocation(500,300);
        masterPasswordFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel mainPanel = new JPanel();

        JPasswordField masterPasswordField = new JPasswordField(10);
        masterPasswordField.setEchoChar('*');
        JLabel passwordLabel = new JLabel("Enter Password: ");
        JButton okayButton = new JButton("Check");
        okayButton.addActionListener(this);

        mainPanel.add(passwordLabel);
        mainPanel.add(masterPasswordField);
        mainPanel.add(okayButton);

        masterPasswordFrame.add(mainPanel);
        masterPasswordFrame.setVisible(true);
        masterPasswordFrame.pack();

        enteredPassword = new String(masterPasswordField.getPassword());    
    }

    public void actionPerformed(ActionEvent event)
    {
        if(enteredPassword.equals(masterPassword))
        {
            JOptionPane.showConfirmDialog(null, "Correct");
            //close the masterPasswordFrame and take the user to the Password Vault
        }
        else
        {
            JOptionPane.showConfirmDialog(null, "Password Incorrect. Please Try Again.");
        }
    }
}

I want the code to output whether the entered password matches the master password but instead, it outputs nothing.

2

There are 2 best solutions below

0
On

This is happening because of this line in the constructor:-

enteredPassword = new String(masterPasswordField.getPassword());

You are taking the value when it is just created, so the value will be blank.

You should take private JPasswordField masterPasswordField; at the class level and then get the value when the button is clicked:-

public void actionPerformed(ActionEvent event)
{
    if(new String(masterPasswordField.getPassword()).equals(masterPassword))
    {
.
. 

And you can get rid of enteredPassword variable.

0
On

You need calling getPassword() in actionPerformed() method.

 public void actionPerformed(ActionEvent event)
    {
        enteredPassword = new String(masterPasswordField.getPassword());
        if(enteredPassword.equals(masterPassword))
        {
            JOptionPane.showConfirmDialog(null, "Correct");
            //close the masterPasswordFrame and take the user to the Password Vault
        }
        else
        {
            JOptionPane.showConfirmDialog(null, "Password Incorrect. Please Try Again.");
        }
    }