fxml loader exception “root value is already speci

2019-08-19 19:18发布

I am trying to have a hyperlink open a new window. I have copied the fxml loader method from other parts of my code that work without issue. For some reason I am getting an fxml loader exception issue that says the root value is already specified. I have not specified a root value anywhere in my code. I also have not specified a controller in the fxml because other answers on stackoverflow said this might be causing the issue.

this is the class that is loading the new Window. the issue occurs when trying to load "/SellerProfileRegisterLayout.fxml"

public class SwitchToSellerProfile {

    @FXML private BorderPane borderPane = new BorderPane();
    @FXML private TextField usernameLogin = new TextField();
    @FXML private PasswordField passwordFieldLogin = new PasswordField();
    @FXML private MainController mainController;
    @FXML private Hyperlink sellerProfileRegisterHyperLink = new Hyperlink();

    public SwitchToSellerProfile(MainController mainController){
        this.mainController = mainController;
    }
    @FXML
    private void initialize(){
        sellerProfileRegisterHyperLink.setOnAction(e -> {
            try {
                FXMLLoader loader = new FXMLLoader(getClass().getResource("/SellerProfileRegisterLayout.fxml"));
                loader.setController(new SellerProfileRegisterController(this.mainController));
            this.mainController.getContentPane().getChildren().clear();
                this.mainController.getContentPane().getChildren().add(loader.load());
    // clicking the error always takes me here to the loader.load()
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
    }
}

the fxml for SwitchToSellerProfile

<BorderPane prefHeight="400.0" prefWidth="550.0" 
            xmlns="http://javafx.com/javafx/8.0.172-ea" 
            xmlns:fx="http://javafx.com/fxml/1">
    <center>
      <Pane prefHeight="200.0" prefWidth="441.0" 
            BorderPane.alignment="CENTER">
        <children>
            <Label alignment="CENTER" contentDisplay="CENTER" 
                   layoutX="162.0" layoutY="71.0" prefHeight="46.0" prefWidth="228.0" 
                   text="Business Profile Sign In">
                <font>
                    <Font size="20.0" />
                </font>
            </Label>
            <TextField fx:id="usernameLogin" layoutX="192.0" layoutY="132.0" promptText="Business Username" />
            <PasswordField fx:id="passwordFieldLogin" layoutX="192.0" layoutY="167.0" promptText="Business Password" />
            <Hyperlink fx:id="forgotPasswordHyperlink" layoutX="218.0" layoutY="200.0" prefHeight="25.0" prefWidth="131.0" text="Forgot Password? " />
            <Label fx:id="incorrectLabel" alignment="CENTER_RIGHT" prefHeight="17.0" prefWidth="190.0" text="Wrong username or password" textFill="RED" visible="false" />
            <Button fx:id="btnLogin" layoutX="234.0" layoutY="235.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="83.0" text="Sign In" />
            <Label layoutX="192.0" layoutY="274.0" text="Don't have one?">
               <font>
                  <Font size="12.0" />
               </font>
            </Label>
            <Hyperlink fx:id="sellerProfileRegisterHyperLink" layoutX="283.0" layoutY="271.0" text="Register here">
               <font>
                  <Font size="12.0" />
               </font>
            </Hyperlink>
        </children>
      </Pane>
   </center>
</BorderPane>

the register controller and its fxml

public class SellerProfileRegisterController {

    private MainController mainController;
    @FXML
    private TextField textField = new TextField();
    @FXML private TextField businessEmail = new TextField();
    @FXML private Button registerBtn = new Button();
    @FXML private Hyperlink goBackLink = new Hyperlink();

    public SellerProfileRegisterController(MainController mainController){
        this.mainController = mainController;
    }
    @FXML private void initialize(){
        registerBtn.setText("Hello"); 
        // just to see if it works hint: it doesn't 
    }
}
<BorderPane prefHeight="400.0" prefWidth="600.0" 
            xmlns="http://javafx.com/javafx/8.0.172-ea" 
            xmlns:fx="http://javafx.com/fxml/1">
     <center>
      <Pane prefHeight="200.0" prefWidth="200.0" 
            BorderPane.alignment="CENTER">
          <children>
              <Label alignment="CENTER" contentDisplay="CENTER" 
                     layoutX="181.0" layoutY="62.0" prefHeight="46.0" prefWidth="225.0" 
                     text="Seller Profile Register">
                  <font>
                      <Font size="20.0" />
                  </font>
              </Label>
            <TextField fx:id="textField" layoutX="210.0" layoutY="121.0" promptText="Business Name" />
            <TextField fx:id="businessEmail" layoutX="210.0" layoutY="162.0" promptText="Business Email Address" />
            <Button layoutX="229.0" layoutY="278.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="130.0" text="Register Account" fx:id="registerBtn" />
            <Hyperlink layoutX="210.0" layoutY="311.0" text="Go back? " fx:id="goBackLink">
               <font>
                  <Font size="10.0" />
               </font></Hyperlink>
            <PasswordField layoutX="210.0" layoutY="200.0" promptText="Password" />
            <PasswordField layoutX="211.0" layoutY="238.0" promptText="Confirm Password" />
          </children>
      </Pane>
     </center>
</BorderPane>

The error message:

 javafx.fxml.LoadException: Root value already specified.
 user/IdeaProjects/FinalStore/target/classes/SellerProfileRegisterLayout.fxml
 user/IdeaProjects/FinalStore/target/classes/SellerProfileRegisterLayout.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.createElement(FXMLLoader.java:2755)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2704)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at Store.SwitchToSellerProfile.lambda$initialize$0(SwitchToSellerProfile.java:38)

line 38 is the loader.load() that I commented on above.

So ideally SellerProfileRegisterController loads instead of a nothing happening and an error message to the console. I'm not sure what's causing this. Thanks!

0条回答
登录 后发表回答