Search code examples
javanullpointerexceptiondispose

Dispose(); causing a null pointer exception error in java


I have created a project with multiple GUI classes. Everything seems to be running fine, however when I try to implement the dispose(); function it provides the following error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.swing.JFrame.dispose()" because "Login.loginframe" is null at Login.actionPerformed(Login.java:123)

Here is the following code:

public class Main {

public static void main(String[] args) {

    Login loginframe = new Login();
    
    
    
    

}

}

import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


public class Login implements ActionListener {
private static JLabel userlabel , success , logo;
private static JTextField userText;
private static JLabel passwordlabel;
private static JPasswordField passwordtext;
private static JButton loginbutton , continuebutton;//, continuebutton2;
private static JFrame loginframe;
private static JPanel panel;




Login(){
    
    
    JFrame loginframe = new JFrame ("Login");
    JPanel panel = new JPanel();
    loginframe.setSize(300, 300);
    loginframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    loginframe.add(panel);
    loginframe.setLocationRelativeTo(null);

    
    panel.setLayout(null);
    
    userlabel = new JLabel("User :");
    userlabel.setBounds(10, 20, 80, 25);
    panel.add(userlabel);
    
    userText = new JTextField();
    userText.setBounds(100, 20, 165, 25);
    panel.add(userText);
    
    passwordlabel = new JLabel ("Password : ");
    passwordlabel.setBounds(10, 50, 80, 25);
    panel.add(passwordlabel);
    
    passwordtext = new JPasswordField ();
    passwordtext.setBounds(100, 50, 165, 25);
    panel.add(passwordtext);
    
    logo = new JLabel();
    logo.setIcon(new ImageIcon("emblem.jpg"));
    logo.setBounds(115, 90, 50, 50);
    panel.add(logo);
    
    
    loginbutton = new JButton("Submit");
    loginbutton.setBounds(100, 200, 80, 25);
    loginbutton.setEnabled(true);
    loginbutton.addActionListener(this);
    panel.add(loginbutton);
    
    success = new JLabel("");
    success.setBounds(85, 130, 150, 80);
    panel.add(success);
    
    
    loginframe.setVisible(true);
    
    continuebutton = new JButton("Continue");
    continuebutton.setBounds(85, 200, 100, 25);
    continuebutton.addActionListener(this);
    continuebutton.setVisible(false);
    continuebutton.setEnabled(false);
    panel.add(continuebutton);
    

    
}

@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
    
    
    String user = userText.getText();
    String password = passwordtext.getText();
    System.out.println(user + ", " + password);
    
    
    if(user.equals("admin") && password.equals("password1")) {
        success.setText("Log in successful!");
        loginbutton.setVisible(false);
        loginbutton.setEnabled(false);
        continuebutton.setVisible(true);
        continuebutton.setEnabled(true);

        if(e.getSource()==continuebutton) {
            userText.setText("");
            passwordtext.setText("");
            new adminmain();
            loginframe.dispose();
            
    }
        }
    
    if(user.equals("user") && password.equals("password")) {
        success.setText("Log in successful!");
        loginbutton.setVisible(false);
        loginbutton.setEnabled(false);
        continuebutton.setVisible(true);
        continuebutton.setEnabled(true);

        if(e.getSource()==continuebutton) {
            userText.setText("");
            passwordtext.setText("");
            new usermain();
            loginframe.dispose();
            
        }
        
    }
    
    
    }

}


Solution

  • This line in your main method is your issue JFrame loginframe = new JFrame ("Login");

    You are assigning the value of loginframe to a local variable within the main method, whereas the actionPerformed method is referring to the class variable loginframe at the top of your Login class.

    By simply changing this line:

    JFrame loginframe = new JFrame ("Login");
    

    To be this, you will now be assigning the value to be a class variable and not run into null pointer issues:

    loginframe = new JFrame ("Login");
    

    Caution: Using a static variable like that is not a good idea if you will have multiple instances of the Login object/class.