0

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 TextField, where you input your name, and a Button that switches to another scene, and the second scene only has a Label that should display Welcome, <input from TextField>.

This is my Controller class:

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("/greeter/greetScene.fxml"));
        Stage window = (Stage) button.getScene().getWindow();
        window.setScene(new Scene(fxmlLoader.load()));
        nameLabel.setText(nameField.getText());
    }
}

This is my main class:

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("/greeter/firstScene.fxml"));
        window.setScene(new Scene(fxmlLoader.load()));
        window.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

However, whenever I press the button, this outputs an error:

Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.Label.setText(String)" because "this.nameLabel" is null
    at com.example.exercise/greeter.Controller.buttonHandle(Controller.java:27)

This is my greetScene.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="186.0" prefWidth="407.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="greeter.Controller">
   <HBox layoutX="70.0" layoutY="79.0" prefHeight="33.0" prefWidth="275.0">
      <Label text="Welcome, ">
         <font>
            <Font name="SFUIDisplay-Regular" size="15.0"/>
         </font>
      </Label>
      <Label fx:id="nameLabel">
         <font>
            <Font name="SFUIText-SemiboldItalic" size="15.0"/>
         </font>
      </Label>
   </HBox>
</AnchorPane>

Edit: Here is my firstScene.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="211.0" prefWidth="318.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="greeter.Controller">
   <children>
      <VBox layoutX="45.0" layoutY="50.0" prefHeight="129.0" prefWidth="229.0" spacing="17.0">
         <children>
            <Label text="Enter your name and start.">
               <font>
                  <Font name="SFUIText-RegularG2" size="14.0" />
               </font>
            </Label>
            <TextField fx:id="nameField">
               <font>
                  <Font name="SFUIDisplay-Regular" size="13.0" />
               </font>
            </TextField>
            <Button fx:id="button" mnemonicParsing="false" onAction="#buttonHandle" text="Start">
               <font>
                  <Font name="SFUIText-RegularG1" size="12.0" />
               </font>
            </Button>
         </children>
      </VBox>
   </children>
</AnchorPane>

How do I fix this? Any help would be much appreciated!

noodles888
  • 31
  • 5
  • 2
    Clarify: you say the file is `greetScene.fxml` but in the code you load `firstScene.fxml`. And if you’re really loading the FXML you posted, `button` would be null (because there’s no element with `fx:id=“button”`), not `nameLabel`. – James_D Oct 21 '21 at 00:27
  • @James_D Oh sorry, I forgot to include my code on firstScene.fxml. The button with the fx:id="button" is included on my firstScene.fxml (will edit the question after writing this) – noodles888 Oct 21 '21 at 00:52
  • 1
    `button` will only be initialized in the controller for `firstScene.fmxl`, and `nameLabel` will only be initialized in the controller for `greetScene.fxml`. You need to pass the value from one controller to the other (and use different classes for each controller to avoid convoluted code). – James_D Oct 21 '21 at 00:56
  • @James_D I think I'm getting the idea of it now, thanks! Just a clarification, do I make different Controller classes for each FXML files that I use? – noodles888 Oct 21 '21 at 01:01
  • Yes, make a different controller class for each FXML file. – James_D Oct 21 '21 at 01:02

0 Answers0