简体   繁体   中英

JAVAFX - Exception caused by Location is not set when loading fxml file

I'm trying to integrate my fxml file to my project, using the following code,

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

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

The program crashes at the second line, throwing this exception,

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

在此处输入图片说明 I also tried to used the exact path (using copy path), but I still got the same error.

Also tried,

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

and also put it in the same folder of my java files, but none works...

I don't know if this is relevant, but here is my iml file,

    <?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>

What causes this and how could I fix it?

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

Thanks,

Henry

The error seems to indicate that the fxml file is being loaded from a wrong location. getClass().getResource loads a resource which per Java Spec for a Resource is identified by a string consisting of sequence of substrings, delimited by (/) followed by resource name. Each substring must ne a valid Java identifier. There is no true guarantee of resource resolution with .. as it's not a valid a identifier.
Am assuming you are not using Maven folder structure, so I would suggest simply keeping the fxml file in the same package as that of Java class that launches it and set the path like "sample.fxml" What do you see when you output getClass().getClassLoader().getResource("sample.fxml")
Also make sure your filename matches with the resource name, no spaces before or after - you can rename and check on the spaces, if any.

I see you are using maven and jetbrains. This exception does indeed come when the fxml file's location is wrong in the code. You are using maven. Maven is searching for resources (like fxml files) in it's resources folder. The maven project's root folder is where pom.xml is. I will reference it as {mavenRoot}. So let's assume, your fxml file is in this path:

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

Then you can use theese two lines to set up your fxml loader:

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

In a maven project, the getResource() will search the {mavenRoot}/src/main/resources folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM