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(
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(
    at com.sun.javafx.application.LauncherImpl$$Lambda$1/ Source)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(
    at javafx.fxml.FXMLLoader.loadImpl(
    at javafx.fxml.FXMLLoader.loadImpl(
    at javafx.fxml.FXMLLoader.loadImpl(
    at javafx.fxml.FXMLLoader.loadImpl(
    at javafx.fxml.FXMLLoader.load(
    at FileActions.start(
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/ Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/ Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(
    at com.sun.javafx.application.PlatformImpl$$Lambda$49/ Source)
    at Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/ Source)

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"));
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" />
    <orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="module-library" scope="TEST">
      <library name="JUnit4">
          <root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.12.jar!/" />
          <root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
        <JAVADOC />
        <SOURCES />
    <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" />

What causes this and how could I fix it?

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



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:


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

FXMLLoader loader1 = new FXMLLoader();

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

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.