I made a small program with JavaFX and Scene Builder.
Here's the controller:
package com.sunflowerseedgame;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.text.Text;
public class GameLogic {
@FXML
private Text moneyCount;
public static int amountOfHelper = 0; // example of a helper would be Kid
public static int amountOfSeeds = 0;
public static int amountOfShells = 0;
public static double amountOfMoney = 0;
public void GetMoney(ActionEvent e) {
amountOfMoney++;
moneyCount.setText("Money = " + amountOfMoney);
}
public static void main(String[] args) {
}
}
And here's the FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: green;" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.sunflowerseedgame.GameLogic">
<children>
<Button layoutX="85.0" layoutY="287.0" mnemonicParsing="false" onAction="#GetMoney" style="-fx-background-color: darkseagreen;" text="Obtain $1" />
<Button layoutX="439.0" layoutY="287.0" mnemonicParsing="false" style="-fx-background-color: darkseagreen;" text="Buy Kid - $7" />
<Button layoutX="174.0" layoutY="230.0" mnemonicParsing="false" style="-fx-background-color: darkseagreen;" text="Grab Seed" />
<Button layoutX="348.0" layoutY="230.0" mnemonicParsing="false" style="-fx-background-color: darkseagreen;" text="Suck Seed" />
<Button layoutX="234.0" layoutY="341.0" mnemonicParsing="false" style="-fx-background-color: darkseagreen;" text="Buy Bag of Seeds - $2" />
<Text layoutX="113.0" layoutY="89.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Seed Sucker">
<font>
<Font name="Consolas" size="62.0" />
</font>
</Text>
<Text layoutX="85.0" layoutY="171.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Seeds = 0" />
<Text id="moneyCount" layoutX="271.0" layoutY="171.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Money = 0" />
<Text layoutX="278.0" layoutY="205.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Kids = 0" />
<Text layoutX="454.0" layoutY="171.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Shells = 0" />
</children>
</AnchorPane>
When I set the ID for moneyCount in Scene Builder, the option to so hasn't already there. This is despite me already making the variable for it. It compiles fine, but when I click the money count button I get this error:
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.text.Text.setText(String)" because "this.moneyCount" is null
at com.sunflowerseedgame/com.sunflowerseedgame.GameLogic.GetMoney(GameLogic.java:18)
(There's more errors, but none of the other ones mention my code)
Does anyone know why I'm getting this?