0

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

enter image description here 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

shjnlee
  • 221
  • 2
  • 6
  • 20

2 Answers2

2

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.

Piyush Mattoo
  • 15,454
  • 6
  • 47
  • 56
  • Thank you for your answer. I forgot to include that test that I tried. I tried it before, and did it again when I read your comment. It does not work. would there be anything relevant in my iml that I might mess up? Will edit my post for it. – shjnlee Aug 07 '17 at 06:21
  • So your fxml and Java class were inside the java package? – Piyush Mattoo Aug 07 '17 at 06:32
  • Yes, they are, I put them in the java package, and calling getClass().getClassLoader().getResource("sample.fxml") but I still have that error. – shjnlee Aug 07 '17 at 07:26
  • I would double check the fxml filename, delete --> copy the content --> paste to a new FXML file and name it properly matching the name used in Java class loading it – Piyush Mattoo Aug 07 '17 at 19:50
1

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.

Chase
  • 106
  • 1
  • 9
  • It's weird that although the iml said it's a maven project, I couldn't find the pom.xml any where in my project (I might set up something when creating the project that I couldn't remember exactly). anyway, I tried that line of code, and run loader1.load() and get crashed right at this place with this exception: Caused by: java.lang.IllegalStateException: Location is not set. at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2428) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2403) – shjnlee Aug 07 '17 at 16:26
  • Check out this: https://stackoverflow.com/questions/17228487/javafx-location-is-not-set-error-message – Chase Aug 07 '17 at 16:58
  • I googled and tried the suggestion on that post but it didn't work. the getResources function somehow always return null. – shjnlee Aug 07 '17 at 17:54
  • @shjnlee Try this: In your IDE, create a brand new FXML project. I never used JetBreans IDE, but Netbeans has new FXML project. So when it is created in your environment, it must have a working example of FXMLLoader. Inspect this newly created project's structure, and method. I hope it will help! Actually this is what I did in Netbeans, when I had the same problem. Also make sure your FXML file is valid and free of any errors. That can also be a problem. – Chase Aug 07 '17 at 18:06
  • it's weird that in a new project it works fine, and I followed to make sure every structure is similar, but this project keep getting NULL when trying to get the resource file. I edited my post and add my project (I deleted my other codes so it only load the fxml now). Do you mind helping me take a look? Thanks. – shjnlee Aug 07 '17 at 19:18
  • I created a new project and the problem is fixed. Thanks. – shjnlee Aug 07 '17 at 21:26