簡體   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