Main class
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
/*
Parent root = FXMLLoader.load(getClass().getResource("graphicorg.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
**/
public class Main extends Application {
Button S1Butt;
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("graphicorg.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Controller that changes through 3 scenes that are circularly connected until the person decides to end the program with scene 3
package application;
import java.io.IOException;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class SceneController {
private Stage stage;
private Scene scene;
private Parent root;
@FXML
private TextField BetField;
@FXML
private Label bal;
public void S1(ActionEvent event) throws IOException {
this.bal.setText("Test Text");
System.out.println(bal);
root = FXMLLoader.load(getClass().getResource("graphicorg.fxml"));
stage = (Stage)(((Node)event.getSource()).getScene().getWindow());
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void S2(ActionEvent event) throws IOException {
//System.out.println(bal);
//this.bal.setText("Test Text");
String test = BetField.getText();
root = FXMLLoader.load(getClass().getResource("Scene2.fxml"));
stage = (Stage)(((Node)event.getSource()).getScene().getWindow());
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void S3(ActionEvent event) throws IOException {
root = FXMLLoader.load(getClass().getResource("Scene3.fxml"));
stage = (Stage)(((Node)event.getSource()).getScene().getWindow());
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void exit(ActionEvent event) {
Platform.exit();
}
}
FXML for the 1st scene
<?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.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SceneController">
<children>
<HBox layoutX="43.0" layoutY="43.0" prefHeight="141.0" prefWidth="520.0">
<children>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@card%20back%20blue.png" />
</image>
</ImageView>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@card%20back%20red.png" />
</image>
</ImageView>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@card%20back%20blue.png" />
</image>
</ImageView>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@card%20back%20red.png" />
</image>
</ImageView>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@card%20back%20blue.png" />
</image>
</ImageView>
</children>
</HBox>
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="43.0" layoutY="215.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@download%20(1).jpeg" />
</image>
</ImageView>
<VBox layoutX="331.0" layoutY="234.0" prefHeight="157.0" prefWidth="105.0">
<children>
<Label fx:id="bal" prefHeight="41.0" prefWidth="80.0" text="Player Balance:">
<font>
<Font name="Impact" size="12.0" />
</font>
</Label>
<TextField fx:id="BetField" onAction="#S2" prefHeight="7.0" prefWidth="100.0" promptText="Enter Bet" text="Enter Bet" />
<Button mnemonicParsing="false" onAction="#S2" text="Enter" />
</children>
</VBox>
</children>
</AnchorPane>
Scene 2 FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SceneController">
<children>
<HBox layoutX="43.0" layoutY="43.0" prefHeight="141.0" prefWidth="520.0">
<children>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Cards/7_of_spades.png" />
</image>
</ImageView>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Cards/4_of_diamonds.png" />
</image>
</ImageView>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Cards/3_of_spades.png" />
</image>
</ImageView>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Cards/2_of_clubs.png" />
</image>
</ImageView>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Cards/4_of_diamonds.png" />
</image>
</ImageView>
</children>
</HBox>
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="43.0" layoutY="215.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Cards/download%20(1).jpeg" />
</image>
</ImageView>
<VBox layoutX="239.0" layoutY="268.0" prefHeight="122.0" prefWidth="259.0">
<children>
<Label prefHeight="50.0" prefWidth="206.0" text="Click the cards that you would like to keep">
<font>
<Font name="Impact" size="12.0" />
</font>
</Label>
<Button mnemonicParsing="false" onAction="#S3" text="Next" />
</children>
</VBox>
</children>
</AnchorPane>
Scene 3 FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SceneController">
<children>
<HBox layoutX="43.0" layoutY="43.0" prefHeight="141.0" prefWidth="520.0">
<children>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Cards/7_of_spades.png" />
</image>
</ImageView>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Cards/4_of_diamonds.png" />
</image>
</ImageView>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Cards/3_of_spades.png" />
</image>
</ImageView>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Cards/2_of_clubs.png" />
</image>
</ImageView>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Cards/4_of_diamonds.png" />
</image>
</ImageView>
</children>
</HBox>
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="43.0" layoutY="215.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@download%20(1).jpeg" />
</image>
</ImageView>
<VBox layoutX="239.0" layoutY="268.0" prefHeight="122.0" prefWidth="259.0">
<children>
<Label text="Royal Flush">
<font>
<Font name="Impact" size="12.0" />
</font>
</Label>
<Label prefHeight="50.0" prefWidth="206.0" text="Play Again">
<font>
<Font name="Impact" size="12.0" />
</font>
</Label>
<HBox prefHeight="100.0" prefWidth="200.0">
<children>
<Button mnemonicParsing="false" onAction="#S1" prefHeight="26.0" prefWidth="118.0" text="Yes" />
<Separator opacity="0.0" prefHeight="8.0" prefWidth="59.0" />
<Button mnemonicParsing="false" onAction="#exit" prefHeight="26.0" prefWidth="122.0" text="No" />
</children>
</HBox>
</children>
</VBox>
</children>
</AnchorPane>
This is the error message:
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.Label.setText(String)" because "this.bal" is null at application.SceneController.S1(SceneController.java:32)
why is it null?
If I use this.bal.setText("Test Text"); in the S2 method from the controller it works however when the first scene is reloaded upon pressing the yes button button in Scene 3 FXML it is set back to the default value "Player Balance:" and not "Test Text".
How can I fix it so that the Text value of bal is permanently set to Test Text instead of having it reset?
Thank You