Reputation: 61
I'm fairly new to JavaFX and apologise in advance if this seems like rather a naive question! So my application has a tabPane initialised in the first controller, that's loaded from the main application class. I made another second controller that's loaded from the first controller, each time the next button is pressed. I'm trying to get the current Tabpane's index from the main controller into the secondary controller like this:
Second Controller.java--
MainTestController mainObject =new MainTestController();
private void removeNullOrDeletedValues() {
System.out.println("size of original list"+ownerList.size());
int originalListSize = ownerList.size();
currentTabIndex = mainObject.findCurrentTabIndex(); //this gives nullPointException
and as marked in the comment above, it gives null point exception , and upon debugging the findCurrentTabIndex(), the inspect window shows mainTabPane as null!
mainTestController.java--
public int findCurrentTabIndex() {
int tabIndex = mainTabPane.getSelectionModel().getSelectedIndex(); //mainTabPane shows null?!
return tabIndex;
}
This looks weird to me, because this same class, before pressing the next button(that loads the second controller) in debug mode gives all the values and other details of the mainTabPane on mouse-hover inspection; like in this method below it works fine and mainTabPane isn't null:
mainTestController.java--
@FXML
private void nextTabAction() throws Exception {
int tabIndex = mainTabPane.getSelectionModel().getSelectedIndex(); //this isn't null on debug inspection
if(tabIndex==0 && dbAddedStatus==false) {
mainTabPane.getSelectionModel().selectNext();
sendOverviewListToDatabase();
CreateEntityTabs(tabIndex);
currentTabHeadChange();
}
Am I missing some important detail like once a controller loses focus and the next one is loaded, the first controller elements all become null?
I know what a nullPointException is and why it occurs. I want to know why the mainTabPane is null only after calling it from the second Controller!
Upvotes: 0
Views: 2051
Reputation: 707
You create a new Controller with MainTestController mainObject =new MainTestController();
, but you need to pass the first Controller to the second.
Add a setter for MainTestController
to SecondController
and call it when you load the FXML-File.
For example: (should be in MainTestController)
FXMLLoader loader = new FXMLLoader(getClass().getResource("second.fxml"));
Pane pane = loader.load();
SecondController controller = (SecondController)loader.getController();
controller.setMainTestController(this);
Upvotes: 1