Search code examples
javajavafxfxmlscenebuilder

"fxmlLoadException: root value already specified" but I never set a root value


I'm trying to open a new window when a hyperlink is clicked. For some reason it worked once and then I could never get it to work again. Every time I run it I get an javafx.fxml.LoadException: Root value already specified. error. The thing is I have not set the root value anywhere.

I tried loader.setRoot(null) but that didn't do anything. I never set the root in my fxml or regular method so I'm not sure where it's getting that.

This is the method for loading a new window

@FXML
private void initialize(){

        messageSupports.setOnMouseClicked(e -> {
            try {
                FXMLLoader loader = new FXMLLoader();
                loader.setLocation(getClass().getResource("/MessagingSupportLayout.fxml"));
             //   Parent root = (Parent) loader.load();
                Stage secondaryStage = new Stage();
                loader.setController(new MessagingSupport(mainController));

                mainController.getContentPane().getChildren().add(loader.load());
                secondaryStage.setTitle("Support");
                secondaryStage.setHeight(500);
                secondaryStage.setWidth(350);

                Scene scene = new Scene(loader.load());

                secondaryStage.setScene(scene);
                secondaryStage.show();

            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
    }
}

here's the fxml for MessagingSupportLayout.fxml.

<BorderPane xmlns="http://javafx.com/javafx"
        prefHeight="400.0" prefWidth="600.0">
</BorderPane>

The expected results are a new window opens. I set the first window's title, height, width in my main class in a start method. I don't declare a root there or in the fxml for that. I can post it if you need to see it. Thanks for the help!


Solution

  • figured it out. the line mainController.getContentPane().getChildren().add(loader.load()); sets the root. deleting that line solves the problem.