Philayyy Philayyy - 1 year ago 242
Java Question

How to pass an object between windows in JavaFX

I am new to java and need help understanding objects. I'm attempting to make an application in javaFx but having trouble to pass objects between the windows. Currently, there is a login window, if the login details are correct via checking the database, the dashboard window opens. However, I now need the

object I just created when pressing the login button in the dashboard window to call its methods from the
class. What I have now gives a null pointer exception.

Adding a constructor to the
class which passes
yields a fxml load exception

So my question is how do you pass an instantiated object to another window to use it's methods?


public class MainGui extends Application {

public void start(Stage primaryStage) throws Exception {

Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
primaryStage.setScene(new Scene(root));;


public static void main(String[] args) {

LoginController for login button

public class LoginController {

BackendInterface backendInterface;

TextField username;

PasswordField password;

Button loginButton;

Label loginLabel;

public void loginButtonPress(){

if (username.getText().isEmpty() == true || password.getText().isEmpty() == true ) {

loginLabel.setText("Please enter data in the fields below");

} else {

//initialises backend interface with username and password
backendInterface = new BackendInterface(username.getText(), password.getText().toCharArray());

//opens a connection to the database

if (backendInterface.getConnectionResponse() == "success"){

//return and print response

//directs the user to the dashboard after successful login

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("dashboard.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setTitle("Welcome " + username.getText());
stage.setScene(new Scene(root1));;

//Closes the login screen window
Stage stage2 = (Stage) loginButton.getScene().getWindow();

} catch (Exception e){

} else {
//warns user of invalid login

DashboardController for button on dashboard

public class DashboardController {

public void doStuff(){

LoginController loginController = new LoginController();




UPDATED SOLUTION based on Clayns response:

First Controller load new page and pass object

FXMLLoader loader = new FXMLLoader();
Parent p = loader.getRoot();
Stage stage = new Stage();
stage.setScene(new Scene(p));

DashboardController dashboardController = loader.getController();

Second Controller retrieve object method

public void setBackendInterface(BackendInterface backendInterface) {
this.backendInterface = backendInterface;

Answer Source

You are creating a new LoginControllerin your DashboardController that has nothing to do with the one that was used for the login.

You should give your DashboardController a method to set the BackendInterface and than in the LoginController use fxmlLoader.getController() to get the DashboardController and pass the BackendInterface to it.

Edit: The answer from fabian works the same way

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download