gpt4 book ai didi

java - actionListener/actionhandler 的问题 java NullPointerException

转载 作者:行者123 更新时间:2023-12-02 06:53:17 24 4
gpt4 key购买 nike

我的问题是,当尝试执行我的代码时,我的actionListener 上不断收到NullPointerException。这是我的登录 View 。经过一些测试后,LoginView 类中出现异常。

public class LoginView extends JDialog {

private static final long serialVersionUID = 1L;
private JTextField tfUsername;
private JPasswordField pfPassword;
private JLabel lbUsername;
private JLabel lbPassword;
private JButton btnLogin;
private JButton btnCancel;
private boolean succeeded;

/*public LoginView(Frame parent) {

super(parent, "Login", true);
setLocationRelativeTo(parent);
}*/

public void createUI(){

JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints cs = new GridBagConstraints();

cs.fill = GridBagConstraints.HORIZONTAL;

lbUsername = new JLabel("Username: ");
cs.gridx = 0;
cs.gridy = 0;
cs.gridwidth = 1;
panel.add(lbUsername, cs);

tfUsername = new JTextField(20);
cs.gridx = 1;
cs.gridy = 0;
cs.gridwidth = 2;
panel.add(tfUsername, cs);

lbPassword = new JLabel("Password: ");
cs.gridx = 0;
cs.gridy = 1;
cs.gridwidth = 1;
panel.add(lbPassword, cs);

pfPassword = new JPasswordField(20);
cs.gridx = 1;
cs.gridy = 1;
cs.gridwidth = 2;
panel.add(pfPassword, cs);
panel.setBorder(new LineBorder(Color.GRAY));

btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener(){


public void actionPerformed(ActionEvent e) {
System.out.println("Login in button is pushed ");

}

});

btnCancel = new JButton("Cancel");
/*btnCancel.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
dispose();
}
});*/
JPanel bp = new JPanel();
bp.add(btnLogin);
bp.add(btnCancel);

getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().add(bp, BorderLayout.PAGE_END);



pack();
setResizable(false);


}

//action listener will be handle in LoginController
public void addLoginListener(ActionListener listenForLoginButton){

btnLogin.addActionListener(listenForLoginButton); //error occurs here

}

public void addCancelListener(ActionListener listenForCancelButton){

btnLogin.addActionListener(listenForCancelButton);// and here

}

//return user-name
public String getUsername() {
return tfUsername.getText().trim();
}

//return password
public String getPassword() {
return new String(pfPassword.getPassword());
}

public boolean isSucceeded() {
return succeeded;
}
}

这是 Controller 及其内部类,即操作处理程序

/*The LoginController class handles the logic behind login validation and and 
* error handling connected to login*/
public class LoginController{
@SuppressWarnings("unused")
private DatabaseConnection databaseConnection; //not using yet
private LoginView loginView;


/*the constructor two parameters databaseConnection and loginView
* true to are MVC frameworke*/
public LoginController(DatabaseConnection databaseConnection, LoginView loginView){

this.databaseConnection = databaseConnection;
this.loginView = loginView;



this.loginView.addLoginListener(new LoginHandler());
this.loginView.addCancelListener(new LoginHandler());
}

public boolean authenticate(String username, String password) {
/* hard-coded user-name and password, because we know what the
* user-name and password is*/

if (username.equals("bob") && password.equals("secret")) {

return true;

}

return false;
}



/***********************/
/*****Inner class*******/
/***********************/

class LoginHandler implements ActionListener{

//an empty constructor
public LoginHandler(){

}

public void actionPerformed(ActionEvent e) {

//this switch will only handle 2 events, login is pushed or cancel is pushed
switch(e.getActionCommand()){

case "btnLogin":

System.out.println("Login in button is pushed ");

break;

case "btnCancel":

System.out.println("cancel in button is pushed ");

break;


}
}
}

/***********************/
/****end inner class****/
/***********************/

}//end class

和错误

Exception in thread "main" java.lang.NullPointerException
at se.bbs.nackademin.views.LoginView.addLoginListener(LoginView.java:94)
at se.bbs.nackademin.controllers.LoginController.<init>(LoginController.java:30)
at se.bbs.nackademin.main.Demo.main(Demo.java:23)

最佳答案

堆栈跟踪显示异常发生在 LoginController 构造函数的这一行:

this.loginView.addLoginListener(new LoginHandler());

这一行什么可能为空?只有一种可能性:this.loginView

该字段之前已使用构造函数的 loginView 参数的值进行了初始化。

this.loginView = loginView;

因此,这仅意味着此构造函数的调用者传递 null,而不是传递 LoginView 实例。

这个构造函数的调用者是谁?堆栈跟踪告诉我们:se.bbs.nackademin.main.Demo.main(Demo.java:23)

所以,错误就在那里。 Demo 类的 main 方法将 null 传递给 LoginController 的构造函数。错误代码位于 Demo.java 中的第 23 行。

关于java - actionListener/actionhandler 的问题 java NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17740762/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com