title: How I fix my Label being null when I try to load a new FXML and set a new Scene for it?
body: <p>I want to make a JavaFX program where there are two scenes (firstScene.fxml and greetScene.fxml), made from FXML using Scene Builder. On the first scene, it has a <strong>TextField</strong>, where you input your name, and a <strong>Button</strong> that switches to another scene, and the second scene only has a <strong>Label</strong> that should display <code>Welcome, &lt;input from TextField&gt;.</code></p>
  <p>This is my Controller class:</p>
  <pre><code>package greeter;
  
  import javafx.event.ActionEvent;
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.control.Label;
  import javafx.scene.control.TextField;
  import javafx.stage.Stage;
  
  import java.io.IOException;
  
  public class Controller {
  
      @FXML
      private TextField nameField;
      @FXML
      private Button button;
      @FXML
      private Label nameLabel;
  
      public void buttonHandle(ActionEvent actionEvent) throws IOException {
          FXMLLoader fxmlLoader = new FXMLLoader(Controller.class.getResource(&quot;/greeter/greetScene.fxml&quot;));
          Stage window = (Stage) button.getScene().getWindow();
          window.setScene(new Scene(fxmlLoader.load()));
          nameLabel.setText(nameField.getText());
      }
  }
  </code></pre>
  <p>This is my main class:</p>
  <pre><code>package greeter;
  
  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Scene;
  import javafx.stage.Stage;
  
  import java.io.IOException;
  
  public class Main extends Application {
  
      @Override
      public void start(Stage window) throws IOException {
          FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource(&quot;/greeter/firstScene.fxml&quot;));
          window.setScene(new Scene(fxmlLoader.load()));
          window.show();
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  <p>However, <strong>whenever I press the button</strong>, this outputs an error:</p>
  <pre><code>Caused by: java.lang.NullPointerException: Cannot invoke &quot;javafx.scene.control.Label.setText(String)&quot; because &quot;this.nameLabel&quot; is null
      at com.example.exercise/greeter.Controller.buttonHandle(Controller.java:27)
  </code></pre>
  <p>This is my greetScene.fxml:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  
  &lt;?import javafx.scene.control.Label?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  &lt;?import javafx.scene.layout.HBox?&gt;
  &lt;?import javafx.scene.text.Font?&gt;
  
  &lt;AnchorPane prefHeight=&quot;186.0&quot; prefWidth=&quot;407.0&quot; xmlns=&quot;http://javafx.com/javafx/17&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot;
              fx:controller=&quot;greeter.Controller&quot;&gt;
     &lt;HBox layoutX=&quot;70.0&quot; layoutY=&quot;79.0&quot; prefHeight=&quot;33.0&quot; prefWidth=&quot;275.0&quot;&gt;
        &lt;Label text=&quot;Welcome, &quot;&gt;
           &lt;font&gt;
              &lt;Font name=&quot;SFUIDisplay-Regular&quot; size=&quot;15.0&quot;/&gt;
           &lt;/font&gt;
        &lt;/Label&gt;
        &lt;Label fx:id=&quot;nameLabel&quot;&gt;
           &lt;font&gt;
              &lt;Font name=&quot;SFUIText-SemiboldItalic&quot; size=&quot;15.0&quot;/&gt;
           &lt;/font&gt;
        &lt;/Label&gt;
     &lt;/HBox&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  <p><strong>Edit:</strong> Here is my firstScene.fxml:</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.control.Label?&gt;
  &lt;?import javafx.scene.control.TextField?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  &lt;?import javafx.scene.layout.VBox?&gt;
  &lt;?import javafx.scene.text.Font?&gt;
  
  &lt;AnchorPane prefHeight=&quot;211.0&quot; prefWidth=&quot;318.0&quot; xmlns=&quot;http://javafx.com/javafx/17&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;greeter.Controller&quot;&gt;
     &lt;children&gt;
        &lt;VBox layoutX=&quot;45.0&quot; layoutY=&quot;50.0&quot; prefHeight=&quot;129.0&quot; prefWidth=&quot;229.0&quot; spacing=&quot;17.0&quot;&gt;
           &lt;children&gt;
              &lt;Label text=&quot;Enter your name and start.&quot;&gt;
                 &lt;font&gt;
                    &lt;Font name=&quot;SFUIText-RegularG2&quot; size=&quot;14.0&quot; /&gt;
                 &lt;/font&gt;
              &lt;/Label&gt;
              &lt;TextField fx:id=&quot;nameField&quot;&gt;
                 &lt;font&gt;
                    &lt;Font name=&quot;SFUIDisplay-Regular&quot; size=&quot;13.0&quot; /&gt;
                 &lt;/font&gt;
              &lt;/TextField&gt;
              &lt;Button fx:id=&quot;button&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#buttonHandle&quot; text=&quot;Start&quot;&gt;
                 &lt;font&gt;
                    &lt;Font name=&quot;SFUIText-RegularG1&quot; size=&quot;12.0&quot; /&gt;
                 &lt;/font&gt;
              &lt;/Button&gt;
           &lt;/children&gt;
        &lt;/VBox&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  <p>How do I fix this? Any help would be much appreciated!</p>