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


这是tha控制器及其内部类,是操作处理程序

/*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行。

10-04 23:46