Java TextArea setText() and appendText() returning Null Pointer Exception


I am using JavaFX 8 and FXML in this project and trying to update my text area with the results from other classes within the program.

The Text Area is defined in the FXML document as such:

<TextArea fx:id="feedBack" editable="false" focusTraversable="false"
layoutX="203.0" layoutY="32.0" prefHeight="205.0" prefWidth="308.0"

It is called in the controller here, note that originally it was a "private" item but to make it work with the classes I made it public:

public static TextArea feedBack;

EDIT: It is worth noting that when the TextArea is identified as "private" I have no issue getting the set/append text method to work but this does not allow me to use the text area in other classes, which is what I need to do.

However now when I try to do either appendText() or setText(), such as follows:

feedBack.setText("Connection Made"); 

I am given a null point exception. Why is this? I have made this work in JavaFX 7 with FXML by doing the same thing and it works fine. What am I missing to make this work?

EDIT, test/proof of brokenness with complete tiny program. I did not make another class for this one as the textarea won't work in the control anyway.


<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="" xmlns:fx="" fx:controller="textareatester.FXMLDocumentController">
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
<TextArea fx:id="feedback" layoutX="61.0" prefHeight="200.0" prefWidth="200.0" wrapText="true" />
  <Button fx:id="dostuff" layoutX="261.0" layoutY="62.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" />

Main Method:

package textareatester;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class TextAreaTester extends Application {

public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

    Scene scene = new Scene(root);


 * @param args the command line arguments
public static void main(String[] args) {

Controller Class:

package textareatester;

import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;

public class FXMLDocumentController implements Initializable {

private Label label;
public static TextArea feedback;
private Button dostuff;

private void handleButtonAction(ActionEvent event) {
    System.out.println("You clicked me!");
   feedback.setText("Hello World!"); ///<-- this is what gives me the NPE

public void initialize(URL url, ResourceBundle rb) {
    // not touching anything

Solution for those who are curious: I ended up simply returning my results strings from my methods and then appending them to the TextArea.

11/5/2014 10:13:37 PM

Accepted Answer

The problem is the static at the definition of the TextArea. Since static attributes are bound to the class and not the object, feedback gets never initiated and is null when accessed in the EventHandler.

11/5/2014 6:05:30 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow