javafx: FXMLLoader.load throws a nullpointerexception

Because i have seen it several times during study at University of applied science Trier (and loading code to the ASB Automatische Software Bewertung), i decided to write few lines. Maybe you can save time.

If you have a java IDE like eclipse or IntelliJ and you write code forJavaFX and you try to get a pre-defined layout from a fxml-File (like you get it from JavaFX Scene Builder 2 or newer), you can use the static load-Method to get in a guilayout.fxml like this:

Pane root = FXMLLoader.load(getClass().getResource("guilayout.fxml"));

In this example you get a Pane-Object, is betted in your file. In several resources, you can read, that the guilayout.fxml must be in the same direcotry like your .class-Files are. Yes. Thats right, but not right enough. If you start your program you will get an Error:

java.lang.NullPointerException: Location is required.

Solution: Just make a copy of your fxml-File and delete it from this folder. Then open your GUI and drag and drop the backuped file directly in your package. Thats the solution. With the advice “the fxml has to be in the same folder like the using class-File” means the documentation the eclipse-folder (and not primary the operating system folders). So just do an d&d and you will see: it works and you have automatically done the copy-command at your os.