0

The problem is .load() function at

Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("/resources/PrimaryScene.fxml"));

Seems like it doesn't see the fxml file. I have done research on many similar questions here and there, but none of them helped. I tried multiple paths or moving fxml to main class etc. Might be some problem with IDE configuration, i don't know what else can i do to make it work.

Im working with IntelliJ Idea Java 1.8(so JavaFX is automatically added). What may be important is the project was created as Empty Project not a JavaFX Project. I tried running another project as JavaFX project and it worked fine.

Im attaching error below:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    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(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
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$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
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 pl.pwr.dpp.App.start(App.java:26)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(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$4(WinApplication.java:186)
    ... 1 more
Exception running application pl.pwr.dpp.App

Process finished with exit code 1
Alky
  • 67
  • 7

1 Answers1

0

Just make sure to put the fxml file into the src/main/resources/ directory of your project.

Also make sure that src/main/resources is marked as the resource root in intelij

Right click on folder -> Mark directory as -> "Resource Root"

Then use

Parent root = FXMLLoader.load(getClass().getResource("/PrimaryScene.fxml"));

Also duplicate of JavaFX and maven: NullPointerException: Location is required

Jonas
  • 159
  • 8
  • 1
    This is a normal java project, not a maven project. – dan1st Mar 18 '20 at 18:56
  • The linked issue has nothing to do with maven. What you actually do is FXMLLoader.load(null), and that is triggering the NullPointerException – Jonas Mar 18 '20 at 19:03
  • Why not? It defininetely seems like maven. And your answer is about `src/main/resources` too. – dan1st Mar 18 '20 at 19:06
  • You're right to be honest i just saw the stacktrace and haven't read the rest of the question. It's still essentialy the same beginner issue just in a different flavor. – Jonas Mar 18 '20 at 19:23