-1

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 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 Answers2

2

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

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.

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

adminController.setLoginController(this);
adminController.loadUser();
Dth
  • 1,916
  • 3
  • 23
  • 34
  • Yes, that was actually causing the problem, all the code is perfect only i just had to initialize my database code after some time, so i resolved my problem with Platform.runlater( – Salman Sabir Feb 17 '17 at 16:54
0

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

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

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.

Binyamin Regev
  • 914
  • 5
  • 19
  • 31