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();
I faced the same problem and want to share a little bit related to it. I’m using java 8 and Netbeans 8.1 and when I created a javafx FXML Application I got this one.
Here are some tips:
When you create new project clean and build your project before you try to run.
If you rename any file (controller, fxml) IDE do not apply changes to other files at least Netbeans is not doing so. So, you have to change those file names in other files manually.
You can define controller either in fxml file or in main class.
If you want to define controller in main class use the method described by @James_D. If you want to define in fxml file than use fx:controller attribute as
fx_controller=”yourProjectName.yourFXMLDocumentControllerName”
and in main class reference it as
Parent root = FXMLLoader.load(getClass().getResource(“yourFXMLFileName.fxml”));
If you think everything is correct but you still getting the error clean and build your project again and try to run.
Hope it would help someone.