title: JavaFX exception loading FXML file
body: <p>I have been working on a desktop application, where I need to use JavaFX. I have created various FXML files in a JavaFX project on eclipse built with Scene Builder. I have no issues loading any FXML file except for this one pesky file (FXMLImgList.fxml).</p>
  
  <p>The main idea is that when a button is pressed, a new window appears. Here is my code for the event handler for that button:</p>
  
  <pre><code>@FXML
  private void handleOnActionButtonImg(ActionEvent e) throws MalformedURLException, URISyntaxException
  {
      FXMLLoader loader = new FXMLLoader(getClass().getResource("/res/FXMLImgList.fxml").toURI().toURL());
      VBox root = null;
      try {
          root = (VBox) loader.load(); // ERROR LOADING HERE
      } catch (IOException e1) {
          e1.printStackTrace();
      }
  
      ImgListController ilc = loader.getController();
      ilc.init(imgUrl);
  
      Scene scene = new Scene(root);
      Stage stage = new Stage();
      stage.initModality(Modality.WINDOW_MODAL);
      stage.initOwner((Stage) vBox.getScene().getWindow());
      stage.setScene(scene);
      stage.setResizable(false);
      stage.setTitle("Elegir imagen");
      stage.show();
  }
  </code></pre>
  
  <p>The project in the package explorer looks like so:</p>
  
  <blockquote>
    <p><a href="https://image.ibb.co/ihGMoS/Capture.png" rel="nofollow noreferrer">https://image.ibb.co/ihGMoS/Capture.png</a> (Can't post images)</p>
  </blockquote>
  
  <p>I get this exception from the try-catch block:</p>
  
  <pre><code>javafx.fxml.LoadException:
  /C:/Users/Ximo/Desktop/UNI/2017-2018%20(UPV)/IPC/EclipseWorkspaceIPC/PL03_Ej2/bin/res/FXMLImgList.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.load(FXMLLoader.java:2409)
      at application.VentanaController.handleOnActionButtonImg(VentanaController.java:108)
      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 sun.reflect.misc.Trampoline.invoke(Unknown Source)
      at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
      at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
      at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
      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.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.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.Node.fireEvent(Node.java:8413)
      at javafx.scene.control.Button.fire(Button.java:185)
      at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
      at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
      at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
      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.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:3757)
      at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
      at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
      at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
      at java.security.AccessController.doPrivileged(Native Method)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:417)
      at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
      at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
      at com.sun.glass.ui.View.notifyMouse(View.java:937)
      at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
      at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
      at java.lang.Thread.run(Unknown Source)
  Caused by: java.lang.IllegalArgumentException: Invalid URL: unknown protocol: c
      at javafx.scene.image.Image.validateUrl(Image.java:1121)
      at javafx.scene.image.Image.&lt;init&gt;(Image.java:659)
      at application.ImgListController.initialize(ImgListController.java:64)
      at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
      ... 61 more
  Caused by: java.net.MalformedURLException: unknown protocol: c
      at java.net.URL.&lt;init&gt;(Unknown Source)
      at java.net.URL.&lt;init&gt;(Unknown Source)
      at java.net.URL.&lt;init&gt;(Unknown Source)
      at javafx.scene.image.Image.validateUrl(Image.java:1115)
      ... 64 more
  Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
      at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
      at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
      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.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.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.Node.fireEvent(Node.java:8413)
      at javafx.scene.control.Button.fire(Button.java:185)
      at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
      at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
      at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
      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.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:3757)
      at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
      at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
      at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
      at java.security.AccessController.doPrivileged(Native Method)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:417)
      at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
      at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
      at com.sun.glass.ui.View.notifyMouse(View.java:937)
      at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
      at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
      at java.lang.Thread.run(Unknown Source)
  Caused by: java.lang.reflect.InvocationTargetException
      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 sun.reflect.misc.Trampoline.invoke(Unknown Source)
      at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
      at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
      ... 48 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 application.VentanaController.handleOnActionButtonImg(VentanaController.java:116)
      ... 58 more
  </code></pre>
  
  <p>What's even stranger, the rest of the FXML files load just fine, for example this one:</p>
  
  <pre><code>@FXML
  private void handleOnActionButtonMod(ActionEvent event) 
  {
      FXMLLoader loader = new FXMLLoader(getClass().getResource("/res/FXMLVentana.fxml"));
      VBox root = null;
      try {
          root = (VBox) loader.load(); // NO ERROR LOADING HERE
      } catch (IOException e) {
          e.printStackTrace();
      }
      VentanaController vac = loader.getController();
      vac.init(observablePersonData, table.getSelectionModel().selectedIndexProperty().getValue());
      Scene scene = new Scene(root);
      Stage stage = new Stage();
      stage.initModality(Modality.WINDOW_MODAL);
      stage.initOwner(this.stage);
      stage.setScene(scene);
      stage.setResizable(false);
      stage.setTitle("Modificar");
      stage.show();
  }
  </code></pre>
  
  <p>The idea behind this event handler is pretty much the same, <em>press button -> window opens</em> and works just fine. I've tried many this like editing the returned URL but nothing seems to work.</p>
  
  <p>I've notice the message on line 67:</p>
  
  <blockquote>
    <p>Caused by: java.lang.IllegalArgumentException: Invalid URL: unknown
    protocol: c</p>
  </blockquote>
  
  <p>However the result of:</p>
  
  <pre><code>    System.out.println(getClass().getResource("/res/FXMLVentana.fxml"));
      System.out.println(getClass().getResource("/res/FXMLImgList.fxml"));
  </code></pre>
  
  <p>Outputs:</p>
  
  <blockquote>
    <p>file:/C:/Users/Ximo/Desktop/UNI/2017-2018%20(UPV)/IPC/EclipseWorkspaceIPC/PL03_Ej2/bin/res/FXMLVentana.fxml</p>
    
    <p>file:/C:/Users/Ximo/Desktop/UNI/2017-2018%20(UPV)/IPC/EclipseWorkspaceIPC/PL03_Ej2/bin/res/FXMLImgList.fxml</p>
  </blockquote>
  
  <p>Here's the code for FXMLImgList.fxml:
      </p>
  
  <pre><code>&lt;?import javafx.geometry.Insets?&gt;
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;?import javafx.scene.layout.HBox?&gt;
  &lt;?import javafx.scene.layout.VBox?&gt;
  
  &lt;VBox fx:id="root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="12.0" xmlns="        http://javafx.com/javafx/8.0.151" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ImgListController"&gt;
     &lt;children&gt;
        &lt;HBox alignment="CENTER_RIGHT" spacing="12.0"&gt;
           &lt;children&gt;
              &lt;Button fx:id="cancelar" mnemonicParsing="false" onAction="#handleOnActionButtonCancelar" text="Cancelar" /&gt;
              &lt;Button fx:id="salvar" mnemonicParsing="false" onAction="#handleOnActionButtonSalvar" text="Salvar" /&gt;
           &lt;/children&gt;
           &lt;VBox.margin&gt;
              &lt;Insets /&gt;
           &lt;/VBox.margin&gt;
        &lt;/HBox&gt;
     &lt;/children&gt;
     &lt;padding&gt;
        &lt;Insets bottom="12.0" left="12.0" right="12.0" top="12.0" /&gt;
     &lt;/padding&gt;
  &lt;/VBox&gt;
  </code></pre>
  
  <p>And here's FXMLVentana.fxml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.geometry.Insets?&gt;
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;?import javafx.scene.control.Label?&gt;
  &lt;?import javafx.scene.control.TextField?&gt;
  &lt;?import javafx.scene.image.ImageView?&gt;
  &lt;?import javafx.scene.layout.HBox?&gt;
  &lt;?import javafx.scene.layout.VBox?&gt;
  
  &lt;VBox fx:id="vBox" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="12.0" xmlns="http://javafx.com/javafx/8.0.151" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.VentanaController"&gt;
     &lt;children&gt;
        &lt;HBox alignment="CENTER" spacing="30.0"&gt;
           &lt;children&gt;
              &lt;Label prefHeight="17.0" prefWidth="53.0" text="DNI" /&gt;
              &lt;TextField fx:id="dni" /&gt;
           &lt;/children&gt;
        &lt;/HBox&gt;
        &lt;HBox alignment="CENTER" spacing="30.0"&gt;
           &lt;children&gt;
              &lt;Label prefHeight="17.0" prefWidth="53.0" text="Nombre" /&gt;
              &lt;TextField fx:id="nombre" /&gt;
           &lt;/children&gt;
        &lt;/HBox&gt;
        &lt;HBox alignment="CENTER" spacing="30.0"&gt;
           &lt;children&gt;
              &lt;Label prefHeight="17.0" prefWidth="52.0" text="Apellidos" /&gt;
              &lt;TextField fx:id="apellidos" /&gt;
           &lt;/children&gt;
        &lt;/HBox&gt;
        &lt;HBox alignment="CENTER" spacing="30.0"&gt;
           &lt;children&gt;
              &lt;Label prefHeight="17.0" prefWidth="52.0" text="Ciudad" /&gt;
              &lt;TextField fx:id="city" /&gt;
           &lt;/children&gt;
        &lt;/HBox&gt;
        &lt;HBox alignment="CENTER" spacing="30.0"&gt;
           &lt;children&gt;
              &lt;Label prefHeight="17.0" prefWidth="52.0" text="Provincia" /&gt;
              &lt;TextField fx:id="province" /&gt;
           &lt;/children&gt;
        &lt;/HBox&gt;
        &lt;HBox alignment="CENTER" spacing="12.0"&gt;
           &lt;children&gt;
              &lt;ImageView fx:id="imgView" fitHeight="80.0" fitWidth="80.0" onMouseClicked="#handleImgMouseClick" pickOnBounds="true" preserveRatio    ="true" /&gt;
          &lt;Button     fx:id="buttonImg" mnemonicParsing="false" onAction="#handleOnActionButtonImg" text="Imagen" /&gt;
       &lt;/children    &gt;
    &lt;/HBox    &gt;
    &lt;    HBox alignment="CENTER_RIGHT" spacing="12.0"&gt;
           &lt;children&gt;
              &lt;Button fx:id="buttonCancelar" mnemonicParsing="false" onAction="#handleOnActionButtonCancelar" text="Cancelar" /&gt;
              &lt;Button fx:id="buttonAction" mnemonicParsing="false" onAction="#handleOnActionButtonAction" text="Salvar" /&gt;
           &lt;/children&gt;
        &lt;/HBox&gt;
     &lt;/children&gt;
     &lt;padding&gt;
        &lt;Insets bottom="12.0" left="30.0" right="30.0" top="12.0" /&gt;
     &lt;/padding&gt;
  &lt;/VBox&gt;
  </code></pre>
  
  <p>In conclusion, I'm all out of ideas and nothing I find onlince seems to solve the issue. Any help is appreciated, thanks!</p>