How to pass an object between windows in JavaFX
<p>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 <code>BackendInterface</code> object I just created when pressing the login button in the dashboard window to call its methods from the <code>BackendInterface</code> class. What I have now gives a null pointer exception. </p>

<p>Adding a constructor to the <code>LoginController</code> class which passes <code>this.backendInterface</code> yields a fxml load exception</p>

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

<p><strong>Main</strong></p>

<pre><code>    public class MainGui extends Application {

        @Override
        public void start(Stage primaryStage) throws Exception {

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

        }


        public static void main(String[] args) {
            launch(args);
        }
    }
</code></pre>

<p>LoginController for login button</p>

<pre><code>    public class LoginController {

        BackendInterface backendInterface;

        @FXML
        TextField username;

        @FXML
        PasswordField password;

        @FXML
        Button loginButton;

        @FXML
        Label loginLabel;

        @FXML
        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
                backendInterface.openConnection();

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

                    //return and print response
                    System.out.println(backendInterface.getConnectionResponse());

                    //directs the user to the dashboard after successful login
                    try{

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

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

                    } catch (Exception e){
                        e.printStackTrace();
                    }

                } else {
                    //warns user of invalid login
                    loginLabel.setText(backendInterface.getConnectionResponse());
                }
            }
        }
    }
</code></pre>

<p>DashboardController for button on dashboard</p>

<pre><code>    public class DashboardController {

        @FXML
        public void doStuff(){

            LoginController loginController = new LoginController();

            loginController.backendInterface.printSomething();


        }

    }
</code></pre>

<p>UPDATED SOLUTION based on Clayns response:</p>

<p><strong>First Controller load new page and pass object</strong></p>

<pre><code>FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("dashboard.fxml"));
loader.load();
Parent p = loader.getRoot();
Stage stage = new Stage();
stage.setScene(new Scene(p));

DashboardController dashboardController = loader.getController();
dashboardController.setBackendInterface(backendInterface);
</code></pre>

<p>Second Controller retrieve object method</p>

<pre><code>public void setBackendInterface(BackendInterface backendInterface) {
        this.backendInterface = backendInterface;
    }
</code></pre>