从另一个控制器获取字符串变量值javafx [duplicate]

时间:2022-07-20 23:39:53

This question already has an answer here:

这个问题在这里已有答案:

i have two controllers a login and main, when logincontroller validates a user then maincontroller holds the control, i tried this method, but not working, the userName variable gives null value.

我有两个控制器登录和主,当logincontroller验证用户然后maincontroller持有控件,我尝试了这个方法,但没有工作,userName变量给出null值。

logincontroller class:

public class loginController implements Initializable{

    private String user;
    public String getUser() {
        return user;
    }


 if((user.equals(loginUsername.getText()) && (pwd.equals(loginPassword.getText())))){
            switch(role){
                 case "Admin": Stage adminStage=new Stage();
          FXMLLoader adminLoader = new FXMLLoader(getClass().getResource("/FXML/Admin/Admin.fxml"));
          Parent adminRoot = adminLoader.load();
          AdminController adminController = (AdminController)adminLoader.getController();
          adminController.setLoginController(this);
}

maincontroller class:

public class AdminController implements Initializable {
    @FXML
    private Label userName;

    public void setLoginController(LoginController loginController) {
        this.loginController = loginController;
    }

    public void initialize(URL url, ResourceBundle rb) {
        loginController =new LoginController();

        try {
            db = new DBConnector();
//            String user = loginController.getUser();
            System.out.println(user);
            String query = "SELECT u_full_name,img FROM Users WHERE u_username='"+user+"'";
            db.setResultSet(query);
            while(db.getResultset().next()){
                userName.setText(db.getResultset().getString(1));
                imageUpload.setImage(new Image("file:"+db.getResultset().getString(2)));
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "can't retreive photo",e.toString(),0);
        }

}

2 个解决方案

#1


1  

The initialize() method is being run when your controller gets initialized by the FXMLLoader, right here:

当你的控制器被FXMLLoader初始化时,正在运行initialize()方法,就在这里:

adminLoader.load();

Obviously this happens before you pass the login controller, and that's why you see the NullPointerException when trying to access the user.

显然,这是在您传递登录控制器之前发生的,这就是您在尝试访问用户时看到NullPointerException的原因。

Move the database code to a separate method, for example loadUser, and invoke it manually:

将数据库代码移动到单独的方法,例如loadUser,并手动调用它:

adminController.setLoginController(this);
adminController.loadUser();

#2


0  

Of course, it will be null, your loginController class is already closed when you are trying to get information from it.

当然,它将为null,当您尝试从中获取信息时,您的loginController类已经关闭。

I am missing your class which extends JavaFX Application class and implements the launch method...

我错过了扩展JavaFX Application类并实现启动方法的类...

You should set the userName/password variables in the loginController class AFTER you tested and received a successful login - this way you will keep the window (stage) open if it fails and you are sure that the username/password are correct.

您应该在您测试并获得成功登录后在loginController类中设置userName / password变量 - 这样,如果窗口(阶段)失败并且您确定用户名/密码正确,您将保持窗口(阶段)打开。

#1


1  

The initialize() method is being run when your controller gets initialized by the FXMLLoader, right here:

当你的控制器被FXMLLoader初始化时,正在运行initialize()方法,就在这里:

adminLoader.load();

Obviously this happens before you pass the login controller, and that's why you see the NullPointerException when trying to access the user.

显然,这是在您传递登录控制器之前发生的,这就是您在尝试访问用户时看到NullPointerException的原因。

Move the database code to a separate method, for example loadUser, and invoke it manually:

将数据库代码移动到单独的方法,例如loadUser,并手动调用它:

adminController.setLoginController(this);
adminController.loadUser();

#2


0  

Of course, it will be null, your loginController class is already closed when you are trying to get information from it.

当然,它将为null,当您尝试从中获取信息时,您的loginController类已经关闭。

I am missing your class which extends JavaFX Application class and implements the launch method...

我错过了扩展JavaFX Application类并实现启动方法的类...

You should set the userName/password variables in the loginController class AFTER you tested and received a successful login - this way you will keep the window (stage) open if it fails and you are sure that the username/password are correct.

您应该在您测试并获得成功登录后在loginController类中设置userName / password变量 - 这样,如果窗口(阶段)失败并且您确定用户名/密码正确,您将保持窗口(阶段)打开。