当我尝试访问使用JavaFX传递的控制器时出现空例外 [英] Null exception when I try to access a controller that I passed using JavaFX
问题描述
我有一个带有倍数Tabs的根布局。从我的主应用程序我打开根布局。我在其中包含了多个带有自己控制器的FXML。
我试图将主控制器传递给其中一个Tabes控制器。
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.
我遇到的问题,一切都按预期工作,但我得到一个 null
当我尝试点击新标签中的操作按钮时出现异常。
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.
<fx:indlue fx:id="myNewTabAnchorPane" source="NewTabFXML.fxml"/>
RootLayout控制器
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
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.
}
我缺少什么?
推荐答案
这是问题所在:
@FXML NewTabController newTabController;
但是应当 myNewTabAnchorPaneController
这不是一个部分小写的类名,但是 fx:id + Controller
连接。
It should be myNewTabAnchorPaneController
which is not a partially lowercased class name, but fx:id + Controller
concatenation.
这篇关于当我尝试访问使用JavaFX传递的控制器时出现空例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!