title: Adding 2nd fxml within pane of 1st fxml
body: <p>I'm brand new to javafx so apologies for the basic question!</p>
  <p>My question is how to add a 2nd fxml inside a pane from the 1st fxml.</p>
  <p>I've got a mini program here.</p>
  <pre><code>public class Main extends Application {
      @Override
      public void start(Stage primaryStage) {
          try {
              
              FXMLLoader loader = new FXMLLoader(getClass().getResource(&quot;/fxml1.fxml&quot;));
              Parent root = loader.load();
              Scene scene = new Scene(root);
              
              primaryStage.setScene(scene);
              primaryStage.show();
          } catch(Exception e) {
              e.printStackTrace();
          }
      }
      
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  <p>and FXML1 controller</p>
  <pre><code>
      @FXML
      private Button button;
  
      @FXML
      private Pane pane;
  
      @FXML
      void load(ActionEvent event) throws IOException {
          Pane fp = FXMLLoader.load(getClass().getResource(&quot;/fxml2.fxml&quot;));
          System.out.println(&quot;This should load fxml2&quot;);
          pane.getChildren().add(fp);
      }
      @Override
      public void initialize(URL arg0, ResourceBundle arg1) {
          // TODO Auto-generated method stub      
      }
  }
  </code></pre>
  <p>I keep getting NPE from the FXML field pane
  Hope this makes any sense</p>
  <p>cheers</p>
  <p>Edit --</p>
  <p>Here's the complete stack trace</p>
  <pre><code>Exception in thread &quot;JavaFX Application Thread&quot; java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
      at javafx.fxml@18/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1857)
      at javafx.fxml@18/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
      at javafx.base@18/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
      at javafx.base@18/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
      at javafx.base@18/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      at javafx.base@18/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
      at javafx.base@18/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
      at javafx.base@18/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at javafx.base@18/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at javafx.base@18/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at javafx.base@18/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at javafx.base@18/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at javafx.base@18/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
      at javafx.base@18/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
      at javafx.base@18/javafx.event.Event.fireEvent(Event.java:198)
      at javafx.graphics@18/javafx.scene.Node.fireEvent(Node.java:8797)
      at javafx.controls@18/javafx.scene.control.Button.fire(Button.java:203)
      at javafx.controls@18/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
      at javafx.controls@18/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
      at javafx.base@18/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
      at javafx.base@18/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
      at javafx.base@18/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
      at javafx.base@18/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      at javafx.base@18/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
      at javafx.base@18/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
      at javafx.base@18/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at javafx.base@18/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at javafx.base@18/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at javafx.base@18/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at javafx.base@18/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at javafx.base@18/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
      at javafx.base@18/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
      at javafx.base@18/javafx.event.Event.fireEvent(Event.java:198)
      at javafx.graphics@18/javafx.scene.Scene$MouseHandler.process(Scene.java:3881)
      at javafx.graphics@18/javafx.scene.Scene.processMouseEvent(Scene.java:1874)
      at javafx.graphics@18/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2607)
      at javafx.graphics@18/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
      at javafx.graphics@18/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
      at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
      at javafx.graphics@18/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
      at javafx.graphics@18/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
      at javafx.graphics@18/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
      at javafx.graphics@18/com.sun.glass.ui.View.handleMouseEvent(View.java:551)
      at javafx.graphics@18/com.sun.glass.ui.View.notifyMouse(View.java:937)
      at javafx.graphics@18/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
      at javafx.graphics@18/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
      at java.base/java.lang.Thread.run(Thread.java:833)
  Caused by: java.lang.reflect.InvocationTargetException
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:568)
      at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
      at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:568)
      at javafx.base@18/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
      at javafx.fxml@18/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
      at javafx.fxml@18/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1852)
      ... 46 more
  Caused by: java.lang.NullPointerException: Cannot invoke &quot;javafx.scene.layout.Pane.getChildren()&quot; because &quot;this.pane&quot; is null
      at application.fxml1_controller.load(fxml1_controller.java:26)
      ... 57 more
  </code></pre>
  <p>and the fxml's</p>
  <p>fxml1</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;?import javafx.scene.layout.BorderPane?&gt;
  &lt;?import javafx.scene.layout.Pane?&gt;
  &lt;?import javafx.scene.text.Text?&gt;
  
  &lt;BorderPane maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot; minHeight=&quot;-Infinity&quot; minWidth=&quot;-Infinity&quot; prefHeight=&quot;400.0&quot; prefWidth=&quot;600.0&quot; xmlns=&quot;http://javafx.com/javafx/18&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;application.fxml1_controller&quot;&gt;
     &lt;top&gt;
        &lt;Text strokeType=&quot;OUTSIDE&quot; strokeWidth=&quot;0.0&quot; text=&quot;This is fxml1&quot; BorderPane.alignment=&quot;CENTER&quot; /&gt;
     &lt;/top&gt;
     &lt;center&gt;
        &lt;Pane fx:id=&quot;pane&quot; prefHeight=&quot;200.0&quot; prefWidth=&quot;200.0&quot; BorderPane.alignment=&quot;CENTER&quot; /&gt;
     &lt;/center&gt;
     &lt;left&gt;
        &lt;Button fx:id=&quot;button&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#load&quot; text=&quot;load fxml2&quot; BorderPane.alignment=&quot;CENTER&quot; /&gt;
     &lt;/left&gt;
  &lt;/BorderPane&gt;
  </code></pre>
  <p>fxml2</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  &lt;?import javafx.scene.text.Text?&gt;
  
  
  &lt;AnchorPane maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot; minHeight=&quot;-Infinity&quot; minWidth=&quot;-Infinity&quot; prefHeight=&quot;400.0&quot; prefWidth=&quot;600.0&quot; xmlns=&quot;http://javafx.com/javafx/18&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;application.fxml2_controller&quot;&gt;
     &lt;children&gt;
        &lt;Text layoutX=&quot;277.0&quot; layoutY=&quot;196.0&quot; strokeType=&quot;OUTSIDE&quot; strokeWidth=&quot;0.0&quot; text=&quot;This is fxml2&quot; /&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>