InvocationTargetException when running a javafx program

前端 未结 6 1912
野的像风
野的像风 2020-11-30 10:05

So this worked in the example from javafx when My pc had jdk 1.7.0 so this may be the new version of FX in java8 however;

I get a nice stack-trace

jf         


        
6条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 10:33

    Your MainController doesn't have a zero-argument constructor. If the FXMLLoader encounters a fx:controller attribute on the root element, it attempts to create an instance of that controller by (effectively) calling the zero-argument constructor of the class specified in the attribute.

    To fix this (the simplest way), remove the fx:controller attribute from the FXML file, and set the controller "by hand" on the FXMLLoader. You need to create an FXMLLoader instance instead of relying on the static load(...) method:

    FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
    loader.setController(new MainController(path));
    Pane mainPane = loader.load();
    

提交回复
热议问题