1

I'm creating a JavaFX application and have been using the maven framework alongside it, so naturally, I put the sample.fxml in the resource folder as such: enter image description here

Nevertheless, even though my code to reach is literally a carbon-copy of a previous project where it did work, in this instance it refuses to accept that an fxml file exists at that location:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

It spits this out:

Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
/C:/Users/David/IdeaProjects/FXRpg/target/classes/fxml/sample.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at sample.Main.start(Main.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1118)
    at javafx.scene.image.Image.<init>(Image.java:620)
    at sample.Controller.initialize(Controller.java:31)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 17 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1110)
    ... 20 more

The POM file doesn't alter anything either:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>JavaFxApplication</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.0.2</version>
        </dependency>
    </dependencies>
</project>
Orange Receptacle
  • 1,173
  • 3
  • 19
  • 40
  • how do you start it? And can you check if target/classes/fxml contains sample.fxml? If not, then maven did not compile it :) (IntelliJ might have put this into /out instead) – Jochen Bedersdorfer Dec 29 '16 at 03:10
  • Is there anything in the pom worth posting, like are you redefining the resource folders? – weston Dec 29 '16 at 03:12
  • Detailed answer here: [How to reference javafx fxml files in resource folder?](http://stackoverflow.com/questions/19602727/how-to-reference-javafx-fxml-files-in-resource-folder) – Robert Altena Dec 29 '16 at 03:12
  • @RobertAltena, looking at your link, this is the correct format and should work. – Orange Receptacle Dec 29 '16 at 03:51
  • @OrangeReceptacle the path seems correct for the loader, seems like the sample.fxml is not, could you share the file context as well please – Naman Dec 29 '16 at 04:17

2 Answers2

0

As detailed in an answer from the link shared by @Robert, the path seems correct for the loader, seems like your sample.fxml is not. So if you try splitting the loading part of your resource in these steps

FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/main.fxml"));
Parent content = loader.load(); 

you will find the failure would be at load and not at getResource. Probably the right way to debug it. Also to look further what is wrong in the .fxml, would need to take a look at the file content.

Source - How to reference javafx fxml files in resource folder?

Community
  • 1
  • 1
Naman
  • 27,789
  • 26
  • 218
  • 353
0

Now I am curious. I have build the project in IntelliJ: Maven, fmxl subdirectory as in the question. The following Main.java worked.

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("fxml/sample.fxml"));
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root));
    primaryStage.show();
}

public static void main(String[] args) {
    launch(args);
}

}

If I move the resource file, I get a different error message:

Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)

And for completeness: The POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>JavaFXApplication</groupId>
<artifactId>JavaFXApplication</artifactId>
<version>1.0-SNAPSHOT</version>

Robert Altena
  • 787
  • 2
  • 11
  • 26