JavaFX and maven: NullPointerException: Location is required


I have been trying to get Maven set up with JavaFX. Even though I was unexperienced with Maven and JavaFX, I didn't expect it to be so much of a challenge. My Java knowledge is pretty solid (including Swing), and didn't expect to have this much difficulty getting it set up.

I started with a JavaFX project supplied by IntelliJ 13.0 Community Edition. The code in my Main class is relatively small:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application
    public void start(Stage primaryStage) throws Exception
        Parent root = FXMLLoader.load(getClass().getResource("../sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));;

    public static void main(String[] args)

and my pom.xml isn't too big either:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""





I'm using the JavaFX Maven Plugin to build the application. After running mvn clean jfx:jar everything seems fine, all builds succeed. But when I try to run the application I get the following error:

Exception in Application start method
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.javafx.main.Main.launchApp(
        at com.javafx.main.Main.main(
Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown So
        at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source)
        at com.sun.javafx.application.LauncherImpl$ Source)
        at Source)
Caused by: java.lang.NullPointerException: Location is required.
        at javafx.fxml.FXMLLoader.load(Unknown Source)
        at javafx.fxml.FXMLLoader.load(Unknown Source)
        at javafx.fxml.FXMLLoader.load(Unknown Source)
        at javafx.fxml.FXMLLoader.load(Unknown Source)
        at javafx.fxml.FXMLLoader.load(Unknown Source)
        at sample.Main.start(
        at com.sun.javafx.application.LauncherImpl$ Source)
        at com.sun.javafx.application.PlatformImpl$ Source)
        at com.sun.javafx.application.PlatformImpl$4$ Source)
        at com.sun.javafx.application.PlatformImpl$4$ Source)
        at Method)
        at com.sun.javafx.application.PlatformImpl$ Source)
        at$ Source)
        at Method)
        at$100(Unknown Source)
        at$3$ Source)
        ... 1 more

After some rough debugging the trouble seems to be in the path I am loading my files from. After hardcoding the sample.fxml to a place on my hard drive, the application runs without any problems. Same goes for the current setup (seen above) when running the application from IntelliJ.

I feel like I have exhausted every resource (including StackOverflow, some very similar errors) but I just can't figure out what exactly is wrong.

2/24/2014 10:23:17 PM

Accepted Answer

Make sure that your sample.fxml is in the src/main/resources/ directory (or a subdirectory). Then you can access the file like this:

Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("sample.fxml"));

Explanation: During the compile phase all resources and classes get copied to target/classes/. So your fxml file resides in this directory and your class in a subdirectory regarding its package name. If you call getClass().getResource("sample.fxml"); the file will be searched relative to the class file which will be this directory: target/classes/sample/.

Calling .getResource() on the classloader sets the relative search path to target/classes/ and therefore your file gets found.

P.S. You could also write:

Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"));
2/25/2014 10:21:30 AM

As @Absurd-Mind already explained, maven will not copy any resource files (like fxml) which resides under the src directory.

If you want to keep the fxml files besides the java source files, you can use the maven resource plugin to copy them:


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow