Moe
Moe

Reputation: 1537

Null exception when I try to access a controller that I passed using JavaFX

I have a root layout with multiples Tabs. From my main App I open the root layout. There I included multiple FXMLs with their own controllers. I am trying to pass the main controller to one of the Tabes controller.

The issue I am having, everything works as expected, but I get a null exception when I try to click on an action button from the new tab.

RootLayout FXML

<fx:indlue fx:id="myNewTabAnchorPane" source="NewTabFXML.fxml"/>

RootLayout Controller

@FXML NewTabController newTabController;

mainTabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>(){
    @override
    public void change(ObservableValue<? extends Tab> observable, Tab oldValue, Tab newValue){
        if(newValue == myTab){
        newTabController.setMyRootController(this);
    }

NewTabController

public void setMyRootController(RootController rootController){
this.rootController = rootController;
System.out.println(rootController.getID); // this prints fine
}

However, if I trigger this action I get blank from the same controller

@FXML 
public void createAction(ActionEvent event) throws IOException{
System.out.println(rootController.getID); // with this I get null value. 
}

What am I missing?

Upvotes: 0

Views: 204

Answers (1)

Sergey Grinev
Sergey Grinev

Reputation: 34528

Here is the problem:

 @FXML NewTabController newTabController;

It should be myNewTabAnchorPaneController which is not a partially lowercased class name, but fx:id + Controller concatenation.

Upvotes: 1

Related Questions