Loader instantiation throws nullpointer in JavaFX

瘦欲@ 提交于 2019-12-02 07:35:36
James_D

FXMLLoader.getController() will return null if you call it before the load() method has been called (because if it hasn't loaded the FXML file, it doesn't know what the controller is).

But this is still not going to do what you want. You want a reference to the controller instance that is associated with the root of the FMXL file that is displayed in the UI. It doesn't help to just get an arbitrary controller instance, whether you do it by getting it from another FXMLLoader or by instantiating it directly.

The controller instance you want is the one you can get by calling loader.getController() in the showOverview() method in your Main class. If you want the controllers to communicate with each other, you need to arrange for the RootLayoutController instance to have a reference to that OverViewController instance.

It might be better not to have the controllers have direct access to each other, but to let them communicate with data in a shared data model. This question has a simple example of this approach.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!