0

Many people in my class have got this error, and I am now the one struggeling with it. I have googled it and it seems like a popular problem with new programmers..

I am trying to start my JavaFX Gui application but

 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:363)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
    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:875)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
    at com.sun.javafx.application.LauncherImpl$$Lambda$53/1598924227.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3201)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091)
    at application.Main.start(Main.java:23)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda$56/662377048.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda$50/1268447657.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda$52/2028564720.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda$51/1851691492.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
     at    com.sun.glass.ui.gtk.GtkApplication.lambda$null$45(GtkApplication.java:126  )
        at com.sun.glass.ui.gtk.GtkApplication$$Lambda$42/584634336.run(Unknown     Source)
    ... 1 more
  Exception running application application.Main

I can see the problem. It cannot find my FXML file and that is the problem, but how do I fix it? What am I doing wrong?

Here is a screenshot of what I have been staring at for almost an hour

Smile
  • 51
  • 1
  • 2
  • 10
  • possible duplicate of [JavaFX "Location is required." even though it is in the same package](http://stackoverflow.com/questions/20507591/javafx-location-is-required-even-though-it-is-in-the-same-package) – alex2410 Feb 17 '15 at 10:28
  • Yes, and that did not help me. The top answer was "Moving the file to the main/resources directory worked.". What file should I move? What is the main/resources directory? I have one package, and I cannot see a directory with that name... – Smile Feb 17 '15 at 10:30
  • Examine [tutorial](http://docs.oracle.com/javafx/2/get_started/jfxpub-get_started.htm) from oracle. – alex2410 Feb 17 '15 at 10:34

2 Answers2

0

Remove the '/' from your File. It won't find "/FXML.fxml".

Juce
  • 341
  • 1
  • 8
  • Otherwise try to split it up like this `FXMLLoader loader = new FXMLLoader(this.getClass().getResource("DefaultFrame.fxml")); Parent root = (Parent) loader.load();` – Juce Feb 17 '15 at 10:45
  • Thanks, but still getting same error. It cannot load the FXML. – Smile Feb 17 '15 at 10:49
  • Hmm is your Controller set in the FXML? There should be the attribute `fx:controller="defaultframe.DefaultFrameController"` inside of the FXML file. If its not, add it to the TopFrame. – Juce Feb 17 '15 at 10:51
  • What is defaultFrame and defaultFrameController? My controller is FXMLController... I am very confused, sorry. – Smile Feb 17 '15 at 10:54
  • Sorry just used my own terms^^ in your file it should be `fx:controller="application.FXMLController"` – Juce Feb 17 '15 at 10:56
  • Tried that, still the same problem. Caused by: java.lang.NullPointerException: Location is required. What is this? It is so frustrating. – Smile Feb 17 '15 at 11:01
  • I added "fx:controller="javafxapplication1.FXMLController"" to my FXML, as you suggested. The error I am getting is: java.lang.NullPointerException: Location is required. My code looks the same, look at my original screenshot. Thank you. – Smile Feb 17 '15 at 11:06
  • So the `Parent root = FXMLLoader.load(getClass().getResource("/FXML.fxml"));` is still the same? You must remove the **'/'** from the filepath. Just write `...getResource("FXML.fxml"));` – Juce Feb 17 '15 at 11:10
  • No, I removed the "/". No worried, I created a whole new testfxmlproject and it is working fine. I have no idea what the problem was but the only thing I can say is that I miss Swing. – Smile Feb 17 '15 at 11:16
0

Why do you added the '/' in your location? It is not necessary because your fxml file is in the same folder and not one below. So remove "/".

Bine
  • 394
  • 4
  • 12
  • Maybe your problem is, that your controller is not set. You want to use your FXML Controller for your FXML right? So you have first to set your controller: FXMLController controller = new FXMLController(): FXMLLoader FXMLLoader=new FXMLLoader(Main.getClass().getResource("FXML.fxml")); FXMLLoader.setController(controller); Parent root = FXMLLoader.load(); – Bine Feb 17 '15 at 10:42
  • without an controller it will be not possible to do any actions with your fxml UI; another possibility is to define your controller in your fxml file like it is done in http://stackoverflow.com/questions/20507591/javafx-location-is-required-even-though-it-is-in-the-same-package – Bine Feb 17 '15 at 10:46
  • Thanks, but still getting same error. It cannot load the FXML. – Smile Feb 17 '15 at 10:52
  • did you have any spaces inside getResource("FXML.fxml")? This could lead to problems – Bine Feb 17 '15 at 11:18