title: Javafx location is required (loading FXML file from another folder/package)
body: <p>I have an issue using Java with FXML files. </p>
  
  <p>I searched for hours now and couldn't find anything that solves my problem, so my last hope is to ask the question for my specific case (I'm aware of question like <a href="https://stackoverflow.com/questions/13785919/error-loading-fxml-files-from-a-folder-other-than-the-bin-folder">this one</a> and others but none really helped me in this regard.</p>
  
  <p>Simple explanation: I have an Eclipse Java Project and my (important for this question) classes are in the package [Project Name]/src/measurements.gui. My FXML file is in the folder [Project Name]/resources.</p>
  
  <p>My class that loads the FXML file ElementsProperties.java looks like this:</p>
  
  <pre><code>import java.io.IOException;
  
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.layout.BorderPane;
  import javafx.stage.Modality;
  import javafx.stage.Stage;
  import javafx.stage.Window;
  
  public class ElementsProperties {
  
    public static void show(Window parent, String title) {
  
      ElementsProperties el = new ElementsProperties();
      BorderPane root = el.loadFXMLFile("resources/TestWindow.fxml");
  
      Stage dialog = new Stage();
      dialog.initOwner(parent);
      dialog.setTitle(title);
      dialog.initModality(Modality.WINDOW_MODAL);
      dialog.setScene(new Scene(root));
      dialog.show();
    }
  
    @SuppressWarnings({ "finally", "static-access" })
    private BorderPane loadFXMLFile(String filePath) {
      BorderPane borderPane = null;
      try {
        borderPane = new BorderPane();
        FXMLLoader fxmlLoader = new FXMLLoader();
        Parent content = fxmlLoader.load(getClass().getResource(filePath));
        borderPane.setCenter(content);
      }
      catch (IOException e) {
        e.printStackTrace();
        System.err.println("Couldn't find the specified file");
      }
      catch(Exception e){
        e.printStackTrace();
      }
      finally {
       return borderPane;
      }
  }}
  </code></pre>
  
  <p>The FXML file is shown as a dialog with the following simple line:
      <code>ElementsProperties.show(parent, "TestWindow");</code></p>
  
  <p>My FXML file (created with the JavaFXSceneBuilder 2.0) looks like this:
  `</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.effect.*?&gt;
  &lt;?import javafx.geometry.*?&gt;
  &lt;?import java.lang.*?&gt;
  &lt;?import java.util.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import javafx.scene.paint.*?&gt;
  &lt;?import javafx.scene.text.*?&gt;
  
  &lt;VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"&gt;
    &lt;children&gt;
      &lt;MenuBar VBox.vgrow="NEVER"&gt;
        &lt;menus&gt;
          &lt;Menu mnemonicParsing="false" text="File"&gt;
            &lt;items&gt;
              &lt;MenuItem mnemonicParsing="false" text="New" /&gt;
              &lt;MenuItem mnemonicParsing="false" text="Open…" /&gt;
              &lt;Menu mnemonicParsing="false" text="Open Recent" /&gt;
              &lt;SeparatorMenuItem mnemonicParsing="false" /&gt;
              &lt;MenuItem mnemonicParsing="false" text="Close" /&gt;
              &lt;MenuItem mnemonicParsing="false" text="Save" /&gt;
              &lt;MenuItem mnemonicParsing="false" text="Save As…" /&gt;
              &lt;MenuItem mnemonicParsing="false" text="Revert" /&gt;
              &lt;SeparatorMenuItem mnemonicParsing="false" /&gt;
              &lt;MenuItem mnemonicParsing="false" text="Preferences…" /&gt;
              &lt;SeparatorMenuItem mnemonicParsing="false" /&gt;
              &lt;MenuItem mnemonicParsing="false" text="Quit" /&gt;
            &lt;/items&gt;
          &lt;/Menu&gt;
          &lt;Menu mnemonicParsing="false" text="Edit"&gt;
            &lt;items&gt;
              &lt;MenuItem mnemonicParsing="false" text="Undo" /&gt;
              &lt;MenuItem mnemonicParsing="false" text="Redo" /&gt;
              &lt;SeparatorMenuItem mnemonicParsing="false" /&gt;
              &lt;MenuItem mnemonicParsing="false" text="Cut" /&gt;
              &lt;MenuItem mnemonicParsing="false" text="Copy" /&gt;
              &lt;MenuItem mnemonicParsing="false" text="Paste" /&gt;
              &lt;MenuItem mnemonicParsing="false" text="Delete" /&gt;
              &lt;SeparatorMenuItem mnemonicParsing="false" /&gt;
              &lt;MenuItem mnemonicParsing="false" text="Select All" /&gt;
              &lt;MenuItem mnemonicParsing="false" text="Unselect All" /&gt;
            &lt;/items&gt;
          &lt;/Menu&gt;
          &lt;Menu mnemonicParsing="false" text="Help"&gt;
            &lt;items&gt;
              &lt;MenuItem mnemonicParsing="false" text="About MyHelloApp" /&gt;
            &lt;/items&gt;
          &lt;/Menu&gt;
        &lt;/menus&gt;
      &lt;/MenuBar&gt;
      &lt;AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS"&gt;
        &lt;children&gt;
          &lt;Label alignment="CENTER" layoutX="155.0" layoutY="177.0" style="&amp;#10;" text="Drag components from Library here…" textAlignment="CENTER" textFill="#9f9f9f" wrapText="false"&gt;
            &lt;font&gt;
              &lt;Font size="18.0" /&gt;
            &lt;/font&gt;
          &lt;/Label&gt;
              &lt;GridPane layoutX="-2.0" layoutY="-3.0" prefHeight="407.0" prefWidth="659.0"&gt;
                &lt;columnConstraints&gt;
                  &lt;ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /&gt;
                  &lt;ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /&gt;
                &lt;/columnConstraints&gt;
                &lt;rowConstraints&gt;
                  &lt;RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /&gt;
                  &lt;RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /&gt;
                  &lt;RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /&gt;
                &lt;/rowConstraints&gt;
                 &lt;children&gt;
                    &lt;HBox prefHeight="134.0" prefWidth="654.0" spacing="20.0" GridPane.rowIndex="1"&gt;
                       &lt;children&gt;
                          &lt;Button mnemonicParsing="false" prefHeight="46.0" prefWidth="60.0" text="Click me" wrapText="true" /&gt;
                          &lt;Button mnemonicParsing="false" prefHeight="47.0" prefWidth="73.0" text="No, click me" wrapText="true" /&gt;
                          &lt;Button mnemonicParsing="false" prefHeight="46.0" prefWidth="120.0" text="Don't you dare click me" wrapText="true" /&gt;
                       &lt;/children&gt;
                       &lt;padding&gt;
                          &lt;Insets bottom="20.0" left="20.0" right="20.0" top="20.0" /&gt;
                       &lt;/padding&gt;
                    &lt;/HBox&gt;
                    &lt;TextField promptText="Type something here..." GridPane.columnIndex="1" GridPane.rowIndex="1"&gt;
                       &lt;GridPane.margin&gt;
                          &lt;Insets left="20.0" right="20.0" /&gt;
                       &lt;/GridPane.margin&gt;
                    &lt;/TextField&gt;
                 &lt;/children&gt;
              &lt;/GridPane&gt;
        &lt;/children&gt;
      &lt;/AnchorPane&gt;
    &lt;/children&gt;
  &lt;/VBox&gt;
  </code></pre>
  
  <p>This is actually all that is needed. If I try to run the programm and make the dialog show up, the Exception </p>
  
  <pre><code>java.lang.NullPointerException: Location is required.
  </code></pre>
  
  <p>is given. If I move the FXML file into the same package as the main class, being measurements.gui, and change the filePath in the ELementsProperties.java class' show-method to <code>"TestWindow.fxml"</code>, it all works fine and I see the created window in my application. But I want to have the fxml file in the seperate resource folder for convenience of inserting other fxml files.</p>
  
  <p>I hope I could explain my problem clearly and you can help me solve this. Any ideas on how to load fxml files from a different package than the main class is? 
  Btw, things I've already tried are:</p>
  
  <ol>
  <li>adding the resource folder to the class path</li>
  <li>set the path to "/resources/TestWindow.fxml" (with the forward slash
  at the beginning</li>
  <li>use <code>getClass().getClassLoader().getResource(filePath)</code> as parameter
  for the FXMLLoader's load method</li>
  </ol>
  
  <p>Thank you in advance.</p>