繁体   English   中英

处置(); 在 java 中导致 null 指针异常错误

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

我创建了一个包含多个 GUI 类的项目。 一切似乎都运行良好,但是当我尝试实现 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)

这是以下代码:

公共 class 主要 {

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();
            
        }
        
    }
    
    
    }

}

您的主要方法中的这一行是您的问题JFrame loginframe = new JFrame ("Login");

您将loginframe的值分配给 main 方法中的局部变量,而 actionPerformed 方法指的是Login class 顶部的全局loginframe

通过简单地删除更改此行:

JFrame loginframe = new JFrame ("Login");

为此,您现在将值分配为全局值,而不是遇到 null 指针问题:

JFrame loginframe = new JFrame ("Login");

当我尝试运行代码时,它给了我这个错误。 所以,如果我是正确的 adminmain() 或 usermain() 来自不同的类。 如果尝试将 In 类导入到主 class。 如果您找到了解决方案,请回复我可以从中学习的解决方案。 谢谢。

Login.java:29: error: cannot find symbol
    login();
    ^

符号:方法 login() 位置:class 登录 Login.java:118:错误:找不到符号 new adminmain(); ^ 符号:class adminmain 位置:class 登录 Login.java:134:错误:找不到符号 new usermain(); ^ 符号:class 用户主位置:class 登录 Login.java:142:错误:不兼容的类型:意外的返回值返回 ex; ^ 4 个错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM