title: JavaFX 8: javafx.fxml.LoadException - fxmlLoader.load() doesn't work, but fxmlLoader.getController() does
body: <p>I have a LoadException when trying to load a controller from a FXML file. Here is the FXML file:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import java.lang.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  
  
  &lt;VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Interface.ChangeProjoOrderController"&gt;
     &lt;children&gt;
        &lt;HBox prefHeight="100.0" prefWidth="200.0"&gt;
           &lt;children&gt;
              &lt;ScrollPane prefHeight="200.0" prefWidth="200.0"&gt;
                &lt;content&gt;
                  &lt;AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0"&gt;
                       &lt;children&gt;
                          &lt;GridPane&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;/GridPane&gt;
                       &lt;/children&gt;
                    &lt;/AnchorPane&gt;
                &lt;/content&gt;
              &lt;/ScrollPane&gt;
              &lt;VBox prefHeight="200.0" prefWidth="100.0"&gt;
                 &lt;children&gt;
                    &lt;Button mnemonicParsing="false" text="Button" /&gt;
                    &lt;Button mnemonicParsing="false" text="Button" /&gt;
                 &lt;/children&gt;
              &lt;/VBox&gt;
           &lt;/children&gt;
        &lt;/HBox&gt;
        &lt;ToolBar prefHeight="40.0" prefWidth="200.0"&gt;
          &lt;items&gt;
              &lt;Button mnemonicParsing="false" text="Button" /&gt;
            &lt;Button mnemonicParsing="false" text="Button" /&gt;
              &lt;Button mnemonicParsing="false" text="Button" /&gt;
              &lt;Button mnemonicParsing="false" text="Button" /&gt;
          &lt;/items&gt;
        &lt;/ToolBar&gt;
        &lt;HBox prefHeight="100.0" prefWidth="200.0"&gt;
           &lt;children&gt;
              &lt;Button mnemonicParsing="false" text="Button" /&gt;
              &lt;Button mnemonicParsing="false" text="Button" /&gt;
           &lt;/children&gt;
        &lt;/HBox&gt;
     &lt;/children&gt;
  &lt;/VBox&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>The Window that loads the FXML is:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>package Interface;
  
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.logging.Logger;
  
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.layout.VBox;
  import javafx.stage.Stage;
  import Engine.PackageLight.Projector;
  
  public class ChangeProjoOrderWindow
  {
   private MainWindowController mainController;
   
   private FXMLLoader fxmlLoader = null;
   private VBox root = null;//TODO uncomment me
   //private Parent root = null;//TODO delete me
   private ChangeProjoOrderController controller;
   private Scene scene;
   private Stage stage;
   
   private static final Logger logUI = Logger.getLogger("UI");
   
   
   ChangeProjoOrderWindow(MainWindowController c)
   {
    mainController = c;
    
    try
    {
     fxmlLoader = new FXMLLoader(getClass().getResource("fxml/projectorOrder.fxml"));
     root = (VBox)fxmlLoader.load();//TODO uncomment me
     //root = (Parent)fxmlLoader.load();//TODO delete me
     
     //XXX DO NOT do this !! The controller will stay null (uninitialized)
     //root = FXMLLoader.load(getClass().getResource("fxml/orderProjo.fxml"));
    }
    catch(IOException e)
    {
     logUI.severe("Error: Cannot load the FXML file 'projectorOrder.fxml'.");
     e.printStackTrace();
    }
    
    controller = fxmlLoader.&lt;ChangeProjoOrderController&gt;getController();
    if(controller == null)
     System.err.println("controller is null");
    scene = new Scene(root);
    
    System.exit(0);
    
    stage = new Stage();
    stage.setOnCloseRequest(we -&gt; 
    {
     // Application termination event
     c.setChangeProjectorOrderDisableStatus(false);
     stage.close();
     logUI.fine("orderProjo terminated.");
    });
    
    mainController.windows.executePreChangeProjectorOrder();
    
    stage.setTitle("Modifier l'ordre des projecteurs");
    stage.setScene(scene);
    stage.show();
    
    controller.setMainController(mainController);
    controller.setStage(stage);
   }
   
   protected void setProjectors(ArrayList&lt;Projector&gt; projectors){controller.setProjectors(projectors);}
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>And here is the Controller:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>package Interface;
  
  import java.net.URL;
  import java.util.ArrayList;
  import java.util.ResourceBundle;
  import java.util.logging.Logger;
  
  import javafx.fxml.FXML;
  import javafx.fxml.Initializable;
  import javafx.scene.control.Button;
  import javafx.scene.control.Label;
  import javafx.scene.layout.GridPane;
  import javafx.stage.Stage;
  import Engine.PackageLight.Projector;
  
  public class ChangeProjoOrderController implements Initializable
  {
   /*@FXML private Button up;
   @FXML private Button down;
   @FXML private Button newProjo;
   @FXML private Button remove;
   @FXML private Button duplicate;
   @FXML private Button edit;
   @FXML private Button ok;
   @FXML private Button cancel;
   
   @FXML private GridPane gridPane;*/
   
   private MainWindowController mainController;
   private Stage stage;
   
   private ArrayList&lt;Projector&gt; projectorList = null;
   
   private static final Logger logUI = Logger.getLogger("UI");
   
   @Override
   public void initialize(URL location, ResourceBundle resources)
   {
    /*cancel.setOnAction(event -&gt;
    {
     logUI.info("cancel");
     mainController.setChangeProjectorOrderDisableStatus(false);
     stage.close();
    });
    
    ok.setOnAction(event -&gt;
    {
     logUI.info("ok");
     mainController.setChangeProjectorOrderDisableStatus(false);
     stage.close();
    });*/
    
    initProjectors();
   }
   
   private void initProjectors()
   {
    for(int i=0; i&lt;projectorList.size(); i++)
    {
     Label curr = new Label(Integer.toString(i));
     GridPane.setColumnIndex(curr, 0);
     GridPane.setRowIndex(curr, i);
    }
   }
   
   protected void setMainController(MainWindowController c){mainController = c;}
   protected void setStage(Stage s){stage = s;}
   protected void setProjectors(ArrayList&lt;Projector&gt; projectors){projectorList = projectors;}
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>And finally, the stack trace:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>INFO [UI] Interface.Windows - changeProjoOrder
    Changing the order of the projectors...
  [0m[31m[1mSEVERE [UI] Interface.ChangeProjoOrderWindow - &lt;init&gt;
    Error: Cannot load the FXML file 'projectorOrder.fxml'.
  [0mjavafx.fxml.LoadException: 
  /home/filou/workspace/java/syncart/bin/Interface/fxml/projectorOrder.fxml
  
   at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
   at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2583)
   at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
   at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2413)
   at Interface.ChangeProjoOrderWindow.&lt;init&gt;(ChangeProjoOrderWindow.java:35)
   at Interface.Windows.changeProjoOrder(Windows.java:151)
   at Interface.MainWindowController.changeProjoOrder(MainWindowController.java:308)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:497)
   at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
   at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:497)
   at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
   at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1767)
   at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
   at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
   at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
   at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
   at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
   at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
   at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
   at javafx.event.Event.fireEvent(Event.java:198)
   at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
   at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
   at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$341(ContextMenuContent.java:1358)
   at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer$$Lambda$398/1949840625.handle(Unknown Source)
   at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
   at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
   at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
   at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
   at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
   at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
   at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
   at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
   at javafx.event.Event.fireEvent(Event.java:198)
   at javafx.scene.Scene$MouseHandler.process(Scene.java:3758)
   at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
   at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
   at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
   at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
   at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
   at java.security.AccessController.doPrivileged(Native Method)
   at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
   at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$357/365619965.get(Unknown Source)
   at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
   at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
   at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
   at com.sun.glass.ui.View.notifyMouse(View.java:927)
   at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
   at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
   at com.sun.glass.ui.gtk.GtkApplication$$Lambda$42/1327763628.run(Unknown Source)
   at java.lang.Thread.run(Thread.java:745)
  Caused by: java.lang.NullPointerException
   at Interface.ChangeProjoOrderController.initProjectors(ChangeProjoOrderController.java:58)
   at Interface.ChangeProjoOrderController.initialize(ChangeProjoOrderController.java:53)
   at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
   ... 61 more
  Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
   at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1770)
   at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
   at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
   at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
   at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
   at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
   at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
   at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
   at javafx.event.Event.fireEvent(Event.java:198)
   at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
   at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
   at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$341(ContextMenuContent.java:1358)
   at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer$$Lambda$398/1949840625.handle(Unknown Source)
   at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
   at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
   at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
   at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
   at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
   at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
   at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
   at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
   at javafx.event.Event.fireEvent(Event.java:198)
   at javafx.scene.Scene$MouseHandler.process(Scene.java:3758)
   at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
   at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
   at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
   at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
   at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
   at java.security.AccessController.doPrivileged(Native Method)
   at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
   at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$357/365619965.get(Unknown Source)
   at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
   at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
   at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
   at com.sun.glass.ui.View.notifyMouse(View.java:927)
   at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
   at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
   at com.sun.glass.ui.gtk.GtkApplication$$Lambda$42/1327763628.run(Unknown Source)
   at java.lang.Thread.run(Thread.java:745)
  Caused by: java.lang.reflect.InvocationTargetException
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:497)
   at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
   at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:497)
   at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
   at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1767)
   ... 46 more
  Caused by: java.lang.NullPointerException: Root cannot be null
   at javafx.scene.Scene.&lt;init&gt;(Scene.java:336)
   at javafx.scene.Scene.&lt;init&gt;(Scene.java:194)
   at Interface.ChangeProjoOrderWindow.&lt;init&gt;(ChangeProjoOrderWindow.java:50)
   at Interface.Windows.changeProjoOrder(Windows.java:151)
   at Interface.MainWindowController.changeProjoOrder(MainWindowController.java:308)
   ... 56 more</code></pre>
  </div>
  </div>
  </p>
  
  <p>Ok. Now the problem seems to come from the line</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>root = (VBox)fxmlLoader.load();//TODO uncomment me</code></pre>
  </div>
  </div>
  </p>
  
  <p>which causes an IOException. But after, the lines</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>controller = fxmlLoader.&lt;ChangeProjoOrderController&gt;getController();
    if(controller == null)
     System.err.println("controller is null");</code></pre>
  </div>
  </div>
  </p>
  
  <p>do not print a "controller is null". So the controller is properly loaded, but the "root" isn't. I've checked the "@FXML" tags (which I commented out finally... to no avail), the "fx:controller" in the FXML file (it is written the same as another file that works, i.e. Interface.xxx), I re-did the window with SceneBuilder 2.0 from scratch to be sure the FXML is well-generated, I tried to set the "root" as a "Parent" or "VBox" and I compared my files with other files that work.</p>
  
  <p>The window that is built do not appear.</p>
  
  <p>I'm using:
  LUbuntu 3.13.0-46-generic,
  JavaFX 8 u40,
  Eclipse Mars Milestone 3 (4.5.0M3) Build id 20141113-0320,
  JavaFx SceneBuilder 2.0-b20</p>
  
  <p>So why have I this exception ? Where does it come from ?</p>
  
  <p>I'm not sure if this is clear, I've been in this code for a long time now :P If any more information is needed, please tell me; I'll provide it.</p>
  
  <p>Thanks</p>