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!