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.

Thiagarajan Ramanathan
  • 1,035
  • 5
  • 24
  • 32
  • possible duplicate of [What's the difference between fx:id and id: in JavaFX?](http://stackoverflow.com/questions/23686325/whats-the-difference-between-fxid-and-id-in-javafx) – James_D Aug 08 '14 at 21:17

1 Answers1

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.

Forager
  • 307
  • 2
  • 10