Netbeans 标准 FXML 应用程序的 JavaFX 错误 - .fxml 文件的空指针

JavaFX Error with Netbeans Standard FXML application - Nullpointer for .fxml file

当我使用应用程序 class 创建一个新的 Netbeans Projekt(JavaFX FXML 应用程序)时,我像往常一样收到一个模板(一个 .fxml、一个控制器 class 和主应用程序)。当我尝试 运行 时,出现异常:

Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
    at com.sun.javafx.application.LauncherImpl.access[=10=]0(LauncherImpl.java:47)
    at com.sun.javafx.application.LauncherImpl.run(LauncherImpl.java:115)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2825)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2809)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2795)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2782)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2771)
    at volltextsuche.Volltextsuche.start(Volltextsuche.java:25)
    [...]

这是我的 start() 方法:

@Override
public void start(Stage stage) throws Exception {
    URL url = getClass().getResource("FXMLDocument.fxml");
    System.out.println(url == null);
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}

不知何故 URL 为空。第 25 行是 Parent root = [...] 部分。 .fxml 文档与字符串的名称完全相同,并且与主应用程序 class 在同一个包中。我没有改变任何东西,只是 URL 的空检查。我在谷歌上搜索了很长时间,但我找不到任何关于这个错误的信息。

好的,我发现了错误,请参阅here。错误不同,但解决方案相同。我在我的项目设置中发现对 dist.jar 的错误引用。正如 link 中回答的问题所述,我必须 CLEAN and build 项目进行第一次构建。损坏的参考由此得到修复。我删除了所有已编译的文件,现在编译器完成了所需的一切并编译了整个项目。

这是一种有效的临时解决方案。

先将资源复制到src文件夹同级目录下

所以如果你的 src 文件夹路径是 C:app/src ,那么将所有资源复制到 C:app

运行再次申请