22

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

jfx-project-run:
Executing E:\workspace\PathFinderApp\dist\run1095471771\PathFinderApp.jar using platform C:\Program Files\Java\jdk1.8.0\jre/bin/java
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
    at java.lang.Thread.run(Thread.java:744)
Caused by: javafx.fxml.LoadException: 
file:/E:/workspace/PathFinderApp/dist/run1095471771/PathFinderApp.jar!/com/rpg/gui/main.fxml:11

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2617)
    at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:104)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:937)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:976)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:216)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:738)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2723)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3191)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3164)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3140)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3120)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3113)
    at com.rpg.gui.GUI.loadMainPane(GUI.java:34)
    at com.rpg.gui.GUI.initialize(GUI.java:20)
    at Main.start(Main.java:20)
    at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837)
    at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
    at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
    ... 1 more
Caused by: java.lang.InstantiationException: com.rpg.gui.MainController
    at java.lang.Class.newInstance(Class.java:418)
    at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:932)
    ... 26 more
Caused by: java.lang.NoSuchMethodException: com.rpg.gui.MainController.<init>()
    at java.lang.Class.getConstructor0(Class.java:2971)
    at java.lang.Class.newInstance(Class.java:403)
    ... 28 more
Exception running application Main
Java Result: 1

basically telling me something is wrong here "com/rpg/gui/main.fxml:11"

and that line is

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="com.rpg.gui.MainController">

so I can figure out that it doesn't like something to do with the main controller, so it has to be somthing to do with loading the fxml and the controller right? but thats about where my ideas end

MainController mainController = new MainController(path);
Pane mainPane = FXMLLoader.load(getClass().getResource("main.fxml"));
Window.setMainController(mainController);
Window.swap(path+"content.fxml");

that's the method it throws everything in anyone experienced in javaFX or know of any changes in java8 that'd do this??

  • 6
    I don't really know anything about JavaFX but looking at the stack trace it seems that it expects `MainController` to have a no-argument constructor (which it doesn't). – Radiodef Apr 17 '14 at 11:52
  • what is `com.rpg.gui.GUI.loadMainPane(GUI.java:34)`? show the code dude –  Apr 17 '14 at 11:53
  • 1
    @parsaporahmad I assume `GUI.java:34` is `Pane mainPane = FXMLLoader.load(getClass().getResource("main.fxml"));`, as included in the OP right now. – Radiodef Apr 17 '14 at 12:00
  • I got rid of the constructor and im using setPath(path) now but its complaining about not seeing the location of content.fxml, should I edit the post above I mean technically speaking its fixed that issue – Matthew Optional Meehan Apr 17 '14 at 12:12
  • I don't think this has anything to do with changing from JavaFX2.2 to Java 8: your code would fail in either. – James_D Apr 17 '14 at 13:02

7 Answers7

21

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();
James_D
  • 201,275
  • 16
  • 291
  • 322
  • what is `FXMLLoadergetClass()` in the above code? i'm new in fx please help – Inzimam Tariq IT Jan 06 '17 at 22:16
  • @InzimamTariqIT Fixed the typo. – James_D Jan 06 '17 at 22:23
  • ok, my code is now working without this. I just clean and build the project and everything is fine. – Inzimam Tariq IT Jan 06 '17 at 22:37
  • if we remove `fx:controller` attribute and add controller by hand we need to add all actions by hand because if there is no reference of controller in fxml file in shows more errors for buttons and other things against which we are defining action in xml. – Inzimam Tariq IT Jan 06 '17 at 22:44
  • @InzimamTariqIT No, you shouldn't have to do that. It will act exactly the same way as if you declare the controller in the FXML. – James_D Jan 06 '17 at 22:46
  • I'm confused about one thing what is `path` while calling controller in the above code? – Inzimam Tariq IT Jan 06 '17 at 22:56
  • I'm using empty constructor as new `MainController();` and it works if I specify path IDE says `require no argument found string.` – Inzimam Tariq IT Jan 06 '17 at 22:58
  • This worked for me. After using above, I went back to using fx:controller in fxml and it worked. But as I switched to other program, fx:controller didn't work I had to repeat the same. I don't know why this is happening, but what happens if same thing happens in deployed application? Isn't the code suppose to work same always? – Ali Feb 21 '20 at 02:58
  • @Ali Unclear what you mean by "as I switched to other program". Either your controller has a no-arg constructor, in which case using `fx:controller` will work (assuming no other problems), or it doesn't, in which case you can't use `fx:controller` (unless you do some moderately complex work with the controller factory). – James_D Feb 21 '20 at 03:07
  • @James_D. I had three files main.java, controller.java and doc.fxml. At first fx: controller wasn't working, then i did what's told above and program run smoothly. Then i changed it back and used fx:controller and it was successful. The I switched to another 3 files, like mainpro.java, fxmlcontroller.java, and document.fxml, I then i had to do the same as above with them also. I hope it's clear now? – Ali Feb 22 '20 at 11:22
5

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:

  1. When you create new project clean and build your project before you try to run.
  2. 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.
  3. 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"));
    
  4. 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.

Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69
4

You used

Pane mainPane = FXMLLoader.load(getClass().getResource("main.fxml"));

Try

Pane mainPane = FXMLLoader.load(getClass().getResource("/main.fxml"));
TerryA
  • 58,805
  • 11
  • 114
  • 143
idzik78
  • 59
  • 1
  • This is not a solution to the issue in the question. If the path was wrong, the error message would be `Location not set` instead of the one posted in the question. – fabian Jan 02 '17 at 14:40
2

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"

jdub1581
  • 659
  • 5
  • 10
  • yeah that didnt seem to change much when I changed that line but without java 8 it was just running with xmlns:fx="http://javafx.com/fxml/1" – Matthew Optional Meehan May 15 '14 at 00:42
  • That would be an entirely different issue. If this was be the case the xml parser would complain, which would lead stacktrace that looks different. `Caused by: java.lang.NoSuchMethodException: com.rpg.gui.MainController.()` tells you the `FXMLLoader` is trying to find a constructor which is not available. – fabian Jan 02 '17 at 14:39
1

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

parsa2820
  • 570
  • 4
  • 13
1
layout.getChildren().addAll(choiceBox, button);

I was trying to add a button to the layout, but I had not defined a button object, it looked like this :

Button button;

When I did this :

button = new Button("Click me");

Everything started working fine.

Ruthless
  • 132
  • 3
  • 15
0

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"));