title: Communication between controllers JavaFX
body: <p>I´m trying to display an image on a new windows when I click on a rectangle of my main stage but I receive the following error: "Exception in thread "JavaFX Application Thread" java.lang.NullPointerException"</p>
  
  <p>First controller (ControllerImpl) initializes second controller (ControllerImage) and calls one of its methods (controllerImage.displayImageSel):</p>
  
  <pre><code>    @FXML
  private ControllerImage controllerImage = new ControllerImage();
  public void rectangleSave(Rectangle r, String imagePath) {
      r.setOnMousePressed((event) -&gt; {
          try {
              Stage imageStage = new Stage();
              FXMLLoader loader = new FXMLLoader(getClass().getResource("/images.fxml"));
  
              Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
              imageStage.setX(0);
              imageStage.setY(0);
              imageStage.setHeight(primScreenBounds.getHeight());
              imageStage.setWidth(primScreenBounds.getWidth() * 0.7);
              imageStage.setTitle("JavaFX Scene Graph Demo");
  
              Scene scene = new Scene(loader.load());
              imageStage.setScene(scene);
              imageStage.show();
  
              controllerImage.displayImageSel(imagePath);
  
          } catch (IOException e) {
              System.out.println("Me cago en el PP");
          }
      });
  }
  </code></pre>
  
  <p>Second controller contains a stack pane and method called by ControllerImpl:</p>
  
  <pre><code>@FXML
  public StackPane Spi;
  public void displayImageSel(String imagePath) {
      Rectangle ri = new Rectangle();
      Spi.getChildren().add(ri);
      Image image = new Image(new File(imagePath).toURI().toString());
      ri.setFill(new ImagePattern(image));
  }
  </code></pre>
  
  <p>Of course Spi is defined in FXML file:</p>
  
  <pre><code>       &lt;StackPane fx:id="Spi"......
  </code></pre>
  
  <p>Problem is ControllerImage can't find StackPane Spi. I´ve been implementing all kind of solutions I found related to this but no one has worked so far. </p>
  
  <p>Thanks for your help!</p>
  
  <p>EDIT: new error message</p>
  
  <pre><code>javafx.fxml.LoadException: Root value already specified.
  </code></pre>
  
  <p>/C:/tutorial-app/tutorial-app/target/resources/main/images.fxml
  /C:/tutorial-app/tutorial-app/target/resources/main/images.fxml</p>
  
  <pre><code>at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
  at javafx.fxml.FXMLLoader.createElement(FXMLLoader.java:2755)
  at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2704)
  at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
  at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
  at tutorial.controller.impl.ControllerImpl.lambda$rectangleSave$1(ControllerImpl.java:146)
  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.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:352)
  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$354(GlassViewEventHandler.java:388)
  at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
  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$148(WinApplication.java:191)
  at java.lang.Thread.run(Thread.java:745)
  </code></pre>