首页 文章

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

提问于
浏览
0

我正在尝试使用以下代码将我的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 .

谢谢,

亨利

2 回答

  • 2

    我看到你正在使用maven和jetbrains . 当fxml文件的位置在代码中出错时,确实会出现此异常 . 你正在使用maven . Maven正在其资源文件夹中搜索资源(如fxml文件) . maven项目的根文件夹是pom.xml所在的位置 . 我将其引用为 . 所以我们假设您的fxml文件位于以下路径中:

    {mavenRoot}/src/main/resources/fxml/sample.fxml
    

    然后你可以使用theese两行来设置你的fxml加载器:

    FXMLLoader loader1 = new FXMLLoader();
    loader1.setLocation(getClass().getResource("/fxml/sample.fxml"));
    

    在maven项目中,getResource()将搜索 {mavenRoot}/src/main/resources 文件夹 .

  • 1

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

相关问题