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
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:
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"));
Hope it would help someone.
In Run >> Edit Configurations
Add this line to VM Options:
--module-path /path/to/JavaFX/lib --add-modules=javafx.controls,javafx.fxml
this worked for me after tones of search
I also encounter that a lot... There seems to be a bug in Scenebuilder/netbeans 8, where upon saving in Scenebuilder, it creates another xmlns:fx="..." so that may be an issue...
Also, looking at fxml's i have using java8, I have these: xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
where I noticed your's is: xmlns:fx="http://javafx.com/fxml"
You used
Pane mainPane = FXMLLoader.load(getClass().getResource("main.fxml"));
Try
Pane mainPane = FXMLLoader.load(getClass().getResource("/main.fxml"));
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();
If Any One does not fix the problem by following answers, The simple way is to create new fxml Empty class then click on edit of the fxml causin the problem and copy the xml code to the new class . Go to the start() methode, change the:
Pane mainPane = FXMLLoader.load(getClass().getResource("main.fxml"));
to
Pane mainPane = FXMLLoader.load(getClass().getResource("newClass.fxml"));