0

我的问题是,在尝试执行我的代码时,我的 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)
4

1 回答 1

2

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

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

这条线有什么可能是空的?只有一种可能:this.loginView

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

this.loginView = loginView;

所以它只是意味着这个构造函数的调用者传递 null 而不是传递一个LoginView实例。

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

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

于 2013-07-19T08:33:34.757 回答