0

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?

DylwinTFTW
  • 11
  • 2
  • I'm not familiar with javafx. But, in general, it's not enough to declare a variable's type. You have to `new` it or use other means of creating / initializing it. – Old Dog Programmer Aug 22 '22 at 22:28
  • 5
    @James You are correct in the general sense, but in JavaFX you virtually never want to manually assign `@FXML`-annotated fields (they are injected for you). – Slaw Aug 22 '22 at 22:29
  • 6
    @DylwinTFTW The problem is you have `id="moneyCount"` instead of `fx:id="moneyCount"` in your FXML file. Check out [What's the difference between fx:id and id: in JavaFX?](https://stackoverflow.com/questions/23686325/whats-the-difference-between-fxid-and-id-in-javafx). – Slaw Aug 22 '22 at 22:34
  • 2
    'Already declaring the variable' has nothing to do with `NullPointerException`. – user207421 Aug 22 '22 at 23:54

0 Answers0