Simple username/password validation problem

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() {    
        userLabel = new JLabel(user);

        userText = new JTextField(20);

        passwordLabel = new JLabel(pass);

        JPasswordField passwordText = new JPasswordField(20); //JPasswordField//

        loginButton = new JButton(login);
        loginButton.setBounds(10, 80, 80, 25);
        thehandler handler = new thehandler();

        setSize(350, 200);
    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");

                JOptionPane.showMessageDialog(null, "bad");

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

Please see this topic:

your string comparison looks a bit off.

1 Like

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:


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

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!

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.

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.

1 Like

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

1 Like

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

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