我的问题是,在尝试执行我的代码时,我的actionListener上不断出现NullPointerException。这是我的登录视图。在一些测试之后,在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;
}
}
这是控制器及其内部类,即动作处理程序
/*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)
答案 0 :(得分:2)
堆栈跟踪表明异常发生在LoginController构造函数的这一行:
this.loginView.addLoginListener(new LoginHandler());
此行可能为空?只有一种可能性:this.loginView
。
此字段之前已使用构造函数的loginView
参数的值进行了初始化。
this.loginView = loginView;
因此,它只是意味着此构造函数的调用者传递null而不是传递LoginView
实例。
这个构造函数的调用者是谁?堆栈跟踪告诉我们:se.bbs.nackademin.main.Demo.main(Demo.java:23)
所以,错误就在那里。类Demo
的主要方法将null传递给LoginController
的构造函数。错误的代码位于Demo.java
的第23行。