【问题标题】:JavaFX NullPointerExcepion @ FXMLLoader.load(...) (URL is null)JavaFX NullPointerExcepion @ FXMLLoader.load(...) (URL 为空)
【发布时间】:2020-06-29 21:52:09
【问题描述】:

问题是 .load() 函数在

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

好像没有看到 fxml 文件。我在这里和那里对许多类似的问题进行了研究,但没有一个有帮助。我尝试了多个路径或将 fxml 移动到主类等。 IDE配置可能有问题,我不知道我还能做些什么来使它工作。

我正在使用 IntelliJ Idea Java 1.8(因此会自动添加 JavaFX)。可能重要的是该项目是作为空项目而不是 JavaFX 项目创建的。我尝试将另一个项目作为 JavaFX 项目运行,它运行良好。

我在下面附加错误:

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

【问题讨论】:

  • 检查您的 build(非源)文件夹(通常是 target/classesbin 或类似的)或您的 jar 文件,以查看 PrimaryScene.fxml 是否在其中,并且它所在的位置。
  • 改用FXMLLoader.load(getClass().getClassLoader().getResource("PrimaryScene.fxml"));...
  • @dan1st 不起作用。将在一个小时内检查你的修复詹姆斯
  • 你能展示你的目录结构吗?
  • 我只是忙 atm 我一个小时的概率:/

标签: java javafx


【解决方案1】:

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

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

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

然后使用

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

也是JavaFX and maven: NullPointerException: Location is required的副本

【讨论】:

  • 这是一个普通的java项目,不是maven项目。
  • 链接问题与maven无关。你实际做的是 FXMLLoader.load(null),这就是触发 NullPointerException
  • 为什么不呢?它绝对看起来像行家。你的回答也是关于src/main/resources
  • 说实话,你说得对,我只是看到了堆栈跟踪,还没有阅读剩下的问题。它本质上仍然是同一个初学者问题,只是风格不同。
猜你喜欢
  • 2014-08-04
  • 2015-05-29
  • 1970-01-01
  • 2021-05-08
  • 1970-01-01
  • 1970-01-01
  • 2017-01-30
  • 2016-06-01
  • 2020-01-15
相关资源
最近更新 更多