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

时间:2022-01-29 17:02:53

When I create a new Netbeans Projekt (JavaFX FXML Application) with application class I receive a template as usual (one .fxml, one controller class and the main application). When I try to run this I get an exception:

当我用应用程序类创建一个新的Netbeans Projekt(JavaFX FXML应用程序)时,我像往常一样接收一个模板(一个.fxml,一个控制器类和主应用程序)。当我尝试运行它时,我得到一个例外:

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$000(LauncherImpl.java:47)
    at com.sun.javafx.application.LauncherImpl$1.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)
    [...]

This is my start() method:

这是我的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();
}

and somehow the URL is null. Line 25 is the Parent root = [...] part. The .fxml document has the exact same name as the Strings tell and is in the same package as the main application class. I didn't change anything, just the null check for the URL. I googled for a long time, but I couldn't find anything regarding this error.

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

  • JDK 8.0_45
  • Netbeans 8.0.2
  • Win 8.1 x64
  • 赢8.1 x64

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

2 个解决方案

#1


Ok, I found the error, see here. The error is differnent but the solution is the same. I found a broken reference to dist.jar in my project settings. As the answered question in the link states I had to CLEAN and build the project for the first build. The broken reference got fixed by that. I deleted all already compiled files and now the compiler does everything needed and compiles the whole project.

好的,我发现了错误,请看这里。错误是不同的,但解决方案是相同的。我在项目设置中发现了对dist.jar的破坏引用。正如链接中回答的问题所述,我必须清理并为第一次构建构建项目。破坏的参考由此得到修复。我删除了所有已编译的文件,现在编译器完成了所需的一切,并编译了整个项目。

#2


This is sort of a temporary solution that works.

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

First copy the resource to the same directory as src folder.

首先将资源复制到与src文件夹相同的目录。

So if your src folder path is C:app/src , then copy all resources to C :app

因此,如果您的src文件夹路径是C:app / src,则将所有资源复制到C:app

Run the application again.

再次运行该应用程序。

#1


Ok, I found the error, see here. The error is differnent but the solution is the same. I found a broken reference to dist.jar in my project settings. As the answered question in the link states I had to CLEAN and build the project for the first build. The broken reference got fixed by that. I deleted all already compiled files and now the compiler does everything needed and compiles the whole project.

好的,我发现了错误,请看这里。错误是不同的,但解决方案是相同的。我在项目设置中发现了对dist.jar的破坏引用。正如链接中回答的问题所述,我必须清理并为第一次构建构建项目。破坏的参考由此得到修复。我删除了所有已编译的文件,现在编译器完成了所需的一切,并编译了整个项目。

#2


This is sort of a temporary solution that works.

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

First copy the resource to the same directory as src folder.

首先将资源复制到与src文件夹相同的目录。

So if your src folder path is C:app/src , then copy all resources to C :app

因此,如果您的src文件夹路径是C:app / src,则将所有资源复制到C:app

Run the application again.

再次运行该应用程序。