0

I have three layers Root layout, home, content (rootLayout.fxml, home.fxml and content.fxml)

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(GenerateReport.class
                .getResource("/skin/rootLayout.fxml"));
        rootLayout = (AnchorPane) loader.load();
        Scene scene = new Scene(rootLayout);


        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(GenerateReport.class
                .getResource("/skin/home.fxml"));
        AnchorPane homeLayout = (AnchorPane) loader.load();
        rootLayout.getChildren().addAll(homeLayout);
        .
        .
        .
        rootLayout.getChildren().addAll(contentLayout);

like this I am adding content layout. In rootLayout.fxml i have a home button. My requiredment is if a user clicks home button then i want content layout to be removed and home layout to be visible.

content.fxml

<AnchorPane id="myContent" ... fx:controller="com.ReportController">

rootLayout.fxml

<AnchorPane id="AnchorPane" .. fx:controller="com.ReportController">
<children>
<Button fx:id="home" onAction="#homeBtn" .../>
</children>
</AnchorPane>

In my Controller (In all the fxml file i am pointing to the same controller) class i created

@FXML
private Button home;

@FXML
private AnchorPane myContent;

@FXML
protected void homeBtn(ActionEvent event) throws Exception {
    System.out.println("click! homeBtn");
    myContent.getChildren().clear();
}

The problem i am facing is i am getting NullPointerException. i.e. myContent.getChildren() is returning null. Could anyone help me in resolving this issue.

1

1 Answer 1

0

You're getting a Null Pointer Exception because Javafx doesn't associate your myContent with the AnchorPane stored in the fxml, and does not attach a reference to that object.

Nodes in fxml files are given their name identifiers by using fx:id. Note, for example, that your @FXML private Button home is marked in the fxml as <Button fx:id="home"...>.

In this case, your @FXML AnchorPane myContent is marked in fxml as <AnchorPane id="myContent"...>, not <AnchorPane fx:id="myContent"...>.

id="" is used only in CSS.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.