title: How to do multi-forming from already-coded FXML files in JavaFX?
body: <p>So I'm creating a project in JavaFX which requires the use of multiple forms, and I decided to create 2 FXML files using SceneBuilder. However, when I need to manipulate the forms using the code, it works for the first form, but produces a NullPointerException for the second form. Currently, I have both forms linked to a single controller, which uses the following method to switch between forms (this method is in the controller):</p>
  <pre><code>private void launch(Stage primaryStage, String form, String title) throws Exception {
          Parent root = FXMLLoader.load(getClass().getResource(form));
          primaryStage.setTitle(title);
          primaryStage.setScene(new Scene(root, 600, 400));
          primaryStage.show();
      }
  </code></pre>
  <p>And the stage is basically obtained from the Main class. The result is that the form will load, but a NullPointerException is produced as follows:</p>
  <pre><code>java.lang.NullPointerException
      at Optimizer.Controller2.displayData(Controller2.java:70)
      at Optimizer.Controller.displayData(Controller.java:190)
      at Optimizer.Controller.loadHandler(Controller.java:63)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:564)
      at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
      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:564)
      at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
      at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
      at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1782)
      at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
      at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
      at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
      at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
      at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
      at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
      at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
      at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
      at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8863)
      at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
      at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
      at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
      at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
      at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
      at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
      at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
      at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
      at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
      at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
      at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
      at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3876)
      at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1300(Scene.java:3604)
      at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1874)
      at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2613)
      at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
      at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
      at java.base/java.security.AccessController.doPrivileged(Native Method)
      at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
      at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
      at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
      at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
      at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
      at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
      at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
      at java.base/java.lang.Thread.run(Thread.java:844)
  </code></pre>
  <p>I've tried using separate controllers for each form, but that will produce a LoadException and IntelliJ won't even show the icon which links the variables for the controls to the fx:id. The second form also won't even load.</p>
  <p>For a minimal reproducible example, here's the Main class:</p>
  <pre><code>package sample;
  
  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.stage.Stage;
  
  public class Main extends Application {
      private static Stage stage;
  
      public static Stage getStage() {
          return stage;
      }
  
      @Override
      public void start(Stage primaryStage) throws Exception{
          stage = primaryStage;
          Parent root = FXMLLoader.load(getClass().getResource(&quot;sample.fxml&quot;));
          primaryStage.setTitle(&quot;Hello World&quot;);
          primaryStage.setScene(new Scene(root, 300, 275));
          primaryStage.show();
      }
  
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  <p>Here's the Controller class:</p>
  <pre><code>package sample;
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.event.ActionEvent;
  import javafx.scene.control.TextField;
  import javafx.stage.Stage;
  
  public class Controller {
      @FXML Button btnNew;
      @FXML TextField txt;
  
      public void switchHandler (ActionEvent evt) {
          try {
              launch(Main.getStage(), &quot;sample2.fxml&quot;, &quot;&quot;);
              txt.setText(&quot;Hello World&quot;);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      private void launch(Stage primaryStage, String form, String title) throws Exception {
          Parent root = FXMLLoader.load(getClass().getResource(form));
          primaryStage.setTitle(title);
          primaryStage.setScene(new Scene(root, 600, 400));
          primaryStage.show();
      }
  }
  </code></pre>
  <p>Here's the first form:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import java.lang.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import javafx.geometry.Insets?&gt;
  &lt;?import javafx.scene.layout.GridPane?&gt;
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;?import javafx.scene.control.Label?&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/8&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;sample.Controller&quot;&gt;
     &lt;children&gt;
        &lt;Button fx:id=&quot;btnNew&quot; onAction=&quot;#switchHandler&quot; layoutX=&quot;213.0&quot; layoutY=&quot;145.0&quot; mnemonicParsing=&quot;false&quot; text=&quot;Button&quot; /&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  <p>And here's the second form:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  
  &lt;?import java.lang.*?&gt;
  &lt;?import java.util.*?&gt;
  &lt;?import javafx.scene.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  
  
  &lt;AnchorPane prefHeight=&quot;400.0&quot; prefWidth=&quot;600.0&quot; xmlns=&quot;http://javafx.com/javafx/8&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;sample.Controller&quot;&gt;
     &lt;children&gt;
        &lt;TextField fx:id=&quot;txt&quot; layoutX=&quot;151.0&quot; layoutY=&quot;156.0&quot; /&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  <p>So the program is supposed to first make the first form pop out, and then go to the second form at the click of the button and then change the text of the TextField in the second form to &quot;Hello World&quot;. The second form pops out as normal, but the line which tries to change the text of the TextField creates a NullPointerException.</p>