Simple username/password validation problem


#1

Hi, I try to learn the basics in GUI, swing and how to handle events. I am trying to make a simple login screen where the username/password can be verified when you press the login button. Everything compiles, but I get a ton of error messages when I try to get both the username and the password to strings. When I try just the user, it works fine, but not with both. I tried to look everywhere to get answers, but the programs that are described are either not using a username or it’s way to complicated for my current knowledge. Can someone help me fix this in a simple way ?

import java.util.*;
import javax.swing.* ;
import java.awt.event.*;
import java.awt.*;



public class myPassFrame extends JFrame {
    
    private JLabel userLabel;
    private JTextField userText;
    private JLabel passwordLabel;
    private JPasswordField passwordText;
    private JButton loginButton;
    private static String user = "User";
    private static String pass = "Password";
    private static String login = "login";
    
    
    
    public myPassFrame() {    
        super("Login");
        setLayout(null);
        
        userLabel = new JLabel(user);
       
        userLabel.setBounds(10,20,80,25);
        add(userLabel);

        userText = new JTextField(20);
        userText.setBounds(100,20,165,25);
        add(userText);

        passwordLabel = new JLabel(pass);
        passwordLabel.setBounds(10,50,80,25);
        add(passwordLabel);

      
        JPasswordField passwordText = new JPasswordField(20); //JPasswordField//
        passwordText.setBounds(100,50,165,25);
        add(passwordText);

        loginButton = new JButton(login);
        loginButton.setBounds(10, 80, 80, 25);
        getContentPane().add(loginButton);
        
        thehandler handler = new thehandler();
        userText.addActionListener(handler);
        passwordText.addActionListener(handler);
        loginButton.addActionListener(handler);

        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(350, 200);
        setVisible(true);
     }
     
    public static void main(String[] args){
        myPassFrame frame1 = new myPassFrame();
       
    
    }
     
    private class thehandler implements ActionListener{
        public void actionPerformed(ActionEvent event){
            
            String user = userText.getText();
            String pass = new String(passwordText.getPassword());
            
            if(user == "John" && pass == "Doe"){
                JOptionPane.showMessageDialog(null, "nice");
            }

            else
       
                JOptionPane.showMessageDialog(null, "bad");
        }
 
    }
}

#2

have you looked into swing vs javaFX? i think today javaFX has the preference if i am not mistaken

Please see this topic:

https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java

your string comparison looks a bit off.


#3

Yeah you’re right, I saw this way of doing it while I was searching., but my focus was not on that part. I just put the if statement to show what I wanted to do, but the main problem is not there. If I remove entirely the if statement and I just try to return the username and password in the JOptionPane, I get all kinds of error messages. If I just try to return the username, it works… any idea why and how I can fix this ? Or is there a better way to valid the username/password in the actionPerformed ? Thank you for helping me again by the way! :slight_smile:


#4

here:

JPasswordField passwordText = new JPasswordField(20);

you already defined passwordText here:

private JPasswordField passwordText;

so you shouldn’t enforce JPasswordField data type when create new JPasswordField.

You do it right for userText:

userText = new JTextField(20);

not sure why you do it wrong for your password field, as to why this gives a null-pointer exception, i am not sure. Java is still on my to do list


#5

Great!! It works!!! Never thought the problem was so stupid… I didnt do that intentionally, I just made a mistake there. Thank you very much, you are really helpful!


#6

and please use .equals() for string comparison as well.

I can’t explain why you got a null pointer exception with the faulty code, you would have too google that. My Java knowledge isn’t good enough for that.


#7

Just to further @stetim94 's response your null pointer error comes from this line:

passwordText.getPassword() passwordText refers to the global variable, which is declared but not initialised so its value is null. But stetim has mentioned why it wasn’t being initialised properly.

Also, as he mentioned, it is vital to compare strings with .equals() as they are objects not primitive types.


#8

that makes sense, thank you for the clarification alexcraig :slight_smile:


#9

Thank you very much guys! I used .equals() and everything works perfectly! :slight_smile:


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.