JavaFX NullPointerExcepion@FXMLLoader.load(…)(URL为null)

JavaFX NullPointerExcepion@FXMLLoader.load(…)(URL为null),java,javafx,Java,Javafx,问题是.load()函数位于 Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("/resources/PrimaryScene.fxml")); 似乎没有看到fxml文件。我对许多类似的问题做过研究,但没有一个有用。我尝试了多个路径或将fxml移动到主类等。 IDE配置可能有问题,我不知道我还能做些什么来让它工作 我正在使用IntelliJ Idea Java 1.8(因此自动添加JavaFX)。重要的

问题是.load()函数位于

Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("/resources/PrimaryScene.fxml"));
似乎没有看到fxml文件。我对许多类似的问题做过研究,但没有一个有用。我尝试了多个路径或将fxml移动到主类等。 IDE配置可能有问题,我不知道我还能做些什么来让它工作

我正在使用IntelliJ Idea Java 1.8(因此自动添加JavaFX)。重要的是项目是作为空项目创建的,而不是JavaFX项目。我试着以JavaFX项目的形式运行另一个项目,效果很好

Im附加错误如下:

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

只需确保将fxml文件放入项目的
src/main/resources/
目录

还要确保src/main/resources在intelij中标记为资源根

右键单击文件夹->将目录标记为->“资源根目录”

然后使用

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

这是一个普通的java项目,不是maven项目。链接的问题与maven无关。您实际要做的是fxmloader.load(null),这会触发NullPointerException为什么不呢?它看起来确实像马文。你的答案也是关于
src/main/resources
的。老实说,你是对的,我刚刚看到了stacktrace,没有读剩下的问题。它本质上仍然是相同的初学者问题,只是风格不同。检查构建(而不是源代码)文件夹(通常是
target/classes
bin
或类似文件)或jar文件,查看
primarysecene.fxml
是否在其中,以及它的位置。尝试
fxmloader.load(getClass().getClassLoader().getResource)(“primarysecene.fxml”);
取而代之的是…@dan1st不起作用。将在一小时内检查您的修复程序JamesCan您可以显示您的目录结构吗?我正忙着atm我有一个小时的问题:/