Why am I getting a stackoverflow when loading my fxml?

可紊 提交于 2019-12-02 09:04:44

You should not call FXml loader with in contructor. because when you load fxml file by using FXml loader , it will create MainOverviewTab again and again recursively. so it cause stack overflow error. If you remove the code from constructor and call from explicit method it will work.

public static void mainTabLoader() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("main_overview_tab.fxml"));
    // fxmlLoader.setRoot(content);
    // fxmlLoader.setController(this);      

    try {
        fxmlLoader.load();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

If you call setController(...) on your FXMLLoader instance, you should not specify the controller in the FXML file. Remove the fx:controller attribute from the FXML file and uncomment the setRoot(...) and setController(...) calls and it should work.

This is what happens when a controller constructor tries to load FXML with a fx:controller attribute naming it, it get infinitely recursive. Been there... Clever use of fxmlLoader.setControllerFactory(factoryObject) can get around that.

See my answer for an FXML based control retaining fx:controller attribute in root element, this can be adapted for other controller creation.

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