Issue
I've created a UI with the help of scenebuilder but when im trying to use my components and add/subtract text from them i get NPE.
The errors im having when i try to run my simple code:
Cannot invoke "javafx.scene.control.TextArea.setText(String)" because"this.quoteOfTheDay" is null
at controller.Controller.fillTextAreaWithWebsites(Controller.java:36)
at controller.Main.updateQuotePanel(Main.java:41)
at controller.Main.start(Main.java:22)
I have added my controller class in scenebuilder aswell as checked the spelling of my fxid a million times.
I will gladely add more code and descriptions if needed. package controller;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.IOException;
import java.util.Objects;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
//Path to FXML file
FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("/FXML/mainPage.fxml"));
Parent root = fxmlLoader.load();
updateQuotePanel();
Scene scene = new Scene(root, 613, 702);
primaryStage.setTitle("Test");
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch();
}
public void updateQuotePanel() {
Webscraping webscraping = new Webscraping();
Controller controller = new Controller();
controller.fillTextAreaWithQuotes();
}
}
A simplified example of how my code is structured
public class Controller implements Initializable {
@FXML
private TextArea quoteOfTheDay;
@FXML
public void fillTextAreaWithQuotes() {
quoteOfTheDay.setText("Quotes");
}
}
The code above is supposed to fill a component inside SceneBuilder called TextArea with the text "Quotes" but it doesn't, instead the TextArea, when called, gives me a NPE.
Here is my FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<AnchorPane fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="702.0" prefWidth="612.0" style="-fx-background-color: #C996CC; -fx-background-radius: 1em; -fx-background-color: #476072; -fx-border-radius: 1em;" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller">
<children>
<AnchorPane layoutY="100.0" prefHeight="4.0" prefWidth="613.0" style="-fx-background-color: transparent;" />
<AnchorPane fx:id="menuBar" layoutX="-1.0" prefHeight="112.0" prefWidth="613.0" style="-fx-background-color: #398AB9; -fx-background-radius: 1em;">
<children>
<Button fx:id="removeWebsiteBtn" layoutX="330.0" layoutY="25.0" mnemonicParsing="false" onAction="#removeWebsiteScene" style="-fx-background-color: #398AB9;">
<graphic>
<ImageView fitHeight="50.0" fitWidth="51.0">
<image>
<Image url="@../icons/128trashcanIcon.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="settingsPageBtn" layoutX="70.0" layoutY="25.0" mnemonicParsing="false" onAction="#settingsPageScene" onMouseClicked="#removeWebsiteScene" style="-fx-background-color: #398AB9;">
<graphic>
<ImageView fitHeight="50.0" fitWidth="51.0">
<image>
<Image url="@../icons/128settingsIcon.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="findFileBtn" layoutX="200.0" layoutY="25.0" mnemonicParsing="false" onAction="#findFileScene" style="-fx-background-color: #398AB9;">
<graphic>
<ImageView fitHeight="50.0" fitWidth="51.0">
<image>
<Image url="@../icons/128profilepicpng.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="addWebsiteBtn" layoutX="460.0" layoutY="25.0" mnemonicParsing="false" onAction="#addWebsiteScene" style="-fx-background-color: #398AB9;">
<graphic>
<ImageView fitHeight="50.0" fitWidth="51.0" onMouseClicked="#addWebsiteScene">
<image>
<Image url="@../icons/128additionIcon.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button layoutX="585.0" layoutY="2.0" mnemonicParsing="false" onAction="#exitApplication" style="-fx-background-color: #398AB9;" text="X" />
<Button layoutX="559.0" layoutY="-5.0" mnemonicParsing="false" onAction="#setMinimized" style="-fx-background-color: #398AB9;" text="_" />
</children>
</AnchorPane>
<Pane layoutX="11.0" layoutY="174.0" prefHeight="216.0" prefWidth="308.0" style="-fx-background-radius: 1em; -fx-background-color: white;">
<children>
<TextArea layoutX="4.0" layoutY="4.0" prefHeight="210.0" prefWidth="301.0" style="-fx-border-width: 0;" />
</children>
</Pane>
<Label layoutX="25.0" layoutY="123.0" prefHeight="42.0" prefWidth="280.0" style="-fx-font-size: 24; -fx-text-fill: white;" text="Popular websites 2021:" />
<Pane layoutX="14.0" layoutY="438.0" prefHeight="216.0" prefWidth="308.0" style="-fx-background-radius: 1em; -fx-background-color: white;">
<children>
<TextArea fx:id="quoteOfTheDay" layoutX="4.0" layoutY="4.0" prefHeight="210.0" prefWidth="301.0" style="-fx-border-width: 0;" />
</children>
</Pane>
<Label layoutX="25.0" layoutY="390.0" prefHeight="42.0" prefWidth="319.0" style="-fx-font-size: 24; -fx-text-fill: white;" text="Motivational quote for today:" />
</children>
</AnchorPane>
Solution
You need to use the loaded controller, not create a new one.
Don’t write:
Controller controller = new Controller();
Write:
Controller controller = fxmlLoader.getController();
After the fxml has been loaded.
Answered By - jewelsea
Answer Checked By - David Marino (JavaFixing Volunteer)