-1

Whenever I create a new JavaFX fxml application, every time I try to run it, it throws an InvocationTargetException. It's just the basic application that I try to run without adapting anything...

The fxml file is the default one that only contains a button.

Default code:

public class JavaFXApplication6 extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

Stacktrace:

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:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    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:498)
    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:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at javafxapplication6.JavaFXApplication6.start(JavaFXApplication6.java:22)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    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.lambda$null$147(WinApplication.java:177)
    ... 1 more
Exception running application javafxapplication6.JavaFXApplication6
C:\Users\timge\Documents\NetBeansProjects\JavaFXApplication6\nbproject\build-impl.xml:1052: The following error occurred while executing this line:
C:\Users\timge\Documents\NetBeansProjects\JavaFXApplication6\nbproject\build-impl.xml:806: Java returned: 1
BUILD FAILED (total time: 3 seconds)
Tim Geldof
  • 25
  • 5
  • Did you try [searching for this](https://stackoverflow.com/search?q=%5Bjavafx%5D+java.lang.NullPointerException%3A+Location+is+required.)? – James_D Jun 04 '18 at 15:47
  • Try a clean and rebuild first to see if that resolves your problem. – d.j.brown Jun 04 '18 at 15:49
  • @d.j.brown I've tried this sereral times to no avail... – Tim Geldof Jun 04 '18 at 15:55
  • This simply means that the `FXMLLoader` cannot find the FXML file. Are you sure it is in the correct location? – James_D Jun 04 '18 at 15:58
  • I didn't change a single thing from the default JavaFX FXML application so that isn't even possible I think. – Tim Geldof Jun 04 '18 at 16:03
  • So where is the FXML file? In the same package as the class file you posted? Have you checked that it’s getting deployed? – James_D Jun 04 '18 at 16:23
  • There is no "default" JavaFX application; NetBeans may generate a template application for you, but it would likely still require some work on your part. As James_D says, there is a problem with you `.fxml` filename or path. Check that it is spelled correctly and has not been moved from the `Main` class path. – Zephyr Jun 04 '18 at 16:45
  • it's in the same package as the main class @james_d – Tim Geldof Jun 04 '18 at 22:11
  • @TimGeldof So probably Net Beans is not deploying it. – James_D Jun 04 '18 at 22:24
  • I'm still a novice, how do I make sure Netbeans "deploys it"? – Tim Geldof Jun 04 '18 at 22:49

1 Answers1

0

Maybe this solves your issue: JavaFX "Location is required." even though it is in the same package

Try using getClass().getClassLoader().getResource("FXMLDocument.fxml")

Barry
  • 337
  • 1
  • 2
  • 15
  • Nothing in the question suggests this would be the correct solution. This just seems like a blind guess. – James_D Jun 04 '18 at 15:59