I've got a part of code, which contains all objects of controllers which I've been using in my program. The topButtonsController
is the only one injected from FXML file (fx:include), the others are created by myself.
I recon that initialize()
method is invoked after the constructor of MainController in created, so assuming that all the methods from the initialize()
methods should have do their job firstly.
public class MainController {
@FXML
private TopButtonsController topButtonsController;
private AddNewOrderController addNewOrderController = new AddNewOrderController();
private OrdersController ordersController = new OrdersController();
@FXML
private BorderPane borderPane;
@FXML
public void initialize() {
topButtonsController.setMainController(this);
addNewOrderController.setMainController(this);
ordersController.setMainController(this);
System.out.println("Finished injection");
}
}
Here I've got a class of controller for another FXML file. After pressing the button the acceptButtonClicked
method should invoked, but unfortunately the mainController
is Null! Why it is so if I've already set him to a value in MainController initialize
method, that should have been hit first.
public class AddNewOrderController {
private MainController mainController;
@FXML
private TextField nameOfPizzaTextField;
private ArrayList<Order> arrayList = new ArrayList<Order>();
private ObservableList<Order> observableList = FXCollections.observableArrayList(arrayList);
@FXML
private Button acceptButton;
@FXML
void acceptButtonClicked(ActionEvent event) {
if(mainController == null) {
System.out.println("MainController is null!!"); //it is printed, why?
}
Order order = new Order(nameOfPizzaTextField.getText());
arrayList.add(order);
observableList.add(order);
System.out.println("Dodano "+nameOfPizzaTextField.getText()+"do listy");
System.out.println(observableList);
mainController.getOrdersController().getListView().setItems(mainController.getAddNewOrderController().getObservableList()); // here occurs error
@FXML
public void initialize() {
}
public void setMainController(MainController mainController) {
this.mainController= mainController;
}
}