0

I successfully logged a user in, however, when I save the userID in userLogin(Profile dto) which is in ProfileDataAccessObject.java. I can't seem to access the userID in other Controller or DataAccessObject class.

LoginController.java

public class LoginController implements Initializable {

@FXML private TextField username; // where the user enters their username

@FXML private PasswordField password; // where the user enters the password

@FXML private Button loginBtn; // authenticates users details when clicked

@FXML public Label wrongLogin; // label displayed when login is unsuccessful

@FXML private Hyperlink accountRegistrationLink; // when clicked take user to signup

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {

}

public void userLogin(ActionEvent actionEvent) {

    if (actionEvent.getSource().equals(loginBtn)) {

        Connection connection = DatabaseHandlerSingleton.getDatabaseHandlerSingleton();

        profile = new Profile(username.getText(), password.getText());

        ProfileDataAccessObject profileDataAccessObject = new ProfileDataAccessObject(connection);

        if (profileDataAccessObject.login(profile) == null) {
            System.out.println("Login failed");

        } else {
            //change scene to workspace
            FactorySceneViewCreator.changeScene(actionEvent, "workspace");
        }
    }
}

}

WorkspaceController.java

public class WorkspaceController implements Initializable {

   private LoginController loginController = new LoginController();
   
   @Override
   public void initialize(URL url, ResourceBundle resourceBundle) {
       System.out.println(loginController.profile.toString());
   }
}

Console Output:

 Profile{id=0, email='null', username='null', password='null', user=null, session=null, profilePicturePath='null', encryption=null}
Bantu
  • 11
  • 2
  • 1
    Not sure what you are doing to try to access the user id in the second controller. Create and post a [mre] (surely the database access stuff is irrelevant to the question) that we can copy and run to see what you're doing. [This](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml) might answer your question. – James_D Oct 27 '21 at 00:21
  • I have other tables in the database that reference userID, so I wanted to store it in the model layer and insert it as a foreign key in the database for tables that reference it. I'll check out the link – Bantu Oct 27 '21 at 00:41
  • 1
    Yes, I understand what you want to do. I just don’t see where you’re attempting to do it, so it’s impossible to see where you’re going wrong. Create and post a [mre] that does nothin higher than try to pass a user id from one controller to another. – James_D Oct 27 '21 at 00:42
  • 1
    https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx may also be useful – James_D Oct 27 '21 at 00:44
  • I've created the minimal reproducible example. – Bantu Oct 27 '21 at 01:06
  • 2
    If you create a new `LoginController` instance, then of course it will have its own `profile` instance which won’t be the same as the one you stored the user id in. Pass the profile object to the workspace controller using the technique in the first link. – James_D Oct 27 '21 at 01:19
  • Thanks, the link https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml worked. Much appreciated – Bantu Oct 27 '21 at 02:58

0 Answers0