JAVAFX - 加载 fxml 文件时未设置位置引起的异常

标签 java javafx scenebuilder fxmlloader

我正在尝试使用以下代码将我的 fxml 文件集成到我的项目中,

final FXMLLoader fxmlLoader =
            new FXMLLoader(this.getClass().getResource("/sample.fxml"));

Parent root = (Parent) fxmlLoader.load();

程序在第二行崩溃,抛出此异常,

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
    at com.sun.javafx.application.LauncherImpl$$Lambda$1/1199823423.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3201)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091)
    at FileActions.start(FileActions.java:42)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/970544503.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1878510174.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda$49/1792840102.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/1671111378.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)

Process finished with exit code 130

enter image description here 我也尝试使用确切的路径(使用复制路径),但仍然遇到相同的错误。

也尝试过,

Parent root = FXMLLoader.load(getClass().getResource("../resources/sample.fxml"));
//and
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

并将其放在我的 java 文件的同一文件夹中,但没有任何效果...

我不知道这是否相关,但这是我的 IML 文件,

    <?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="false">
    <output url="file://$MODULE_DIR$/target/classes" />
    <output-test url="file://$MODULE_DIR$/target/test-classes" />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
      <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
      <sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
      <excludeFolder url="file://$MODULE_DIR$/target" />
    </content>
    <orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="module-library" scope="TEST">
      <library name="JUnit4">
        <CLASSES>
          <root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.12.jar!/" />
          <root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
        </CLASSES>
        <JAVADOC />
        <SOURCES />
      </library>
    </orderEntry>
    <orderEntry type="library" exported="" name="commons-net-3.6" level="project" />
    <orderEntry type="library" exported="" name="hamcrest-core-1.3" level="project" />
    <orderEntry type="library" exported="" name="junit-4.12" level="project" />
  </component>
</module>

是什么原因导致此问题以及如何解决它?

Here is a zip of my project if anyone may want to take a look .

谢谢

亨利

最佳答案

该错误似乎表明 fxml 文件是从错误的位置加载的。 getClass().getResource 加载一个资源,该资源符合 Java Spec for a Resource由一个由子字符串序列组成的字符串进行标识,子字符串由 (/) 分隔,后跟资源名称。每个子字符串必须有一个有效的 Java 标识符。 .. 无法真正保证资源解析,因为它不是有效的标识符。
假设您没有使用 Maven 文件夹结构,所以我建议 只需将 fxml 文件保存在与启动它的 Java 类相同的包中,并设置类似 "sample.fxml" 的路径 输出getClass().getClassLoader().getResource("sample.fxml")
时会看到什么 另请确保您的文件名与资源名称匹配,前后没有空格 - 您可以重命名并检查空格(如果有)。

关于JAVAFX - 加载 fxml 文件时未设置位置引起的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45538074/

相关文章:

java - 在 JavaFX Webview 中运行 URLConnection uc

java - 除了javafx中的小动画时间线,还有什么解决办法吗?

java - NAT 连接后面的 RMI 服务器拒绝托管

java - 在 Eclipse 中启动 Websphere 8

java - 如何在关闭阶段后清除 Javafx Webview 内存使用情况

JavaFX 菜单项在右侧显示快捷方式

css - 如何在 FXML 中添加 CSS 样式表

java - 在 WildFly 应用服务器上使用 Netbeans 进行增量部署时出错

java - Integer.parseInt 和带十进制数的字符串格式

javafx-2 - java.lang.IllegalArgumentException : Invalid URL or resource not found