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

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

The text area is defined as such in the FXML document:

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

      

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

@FXML
public static TextArea feedBack;

      

EDIT: It's worth noting that when the TextArea is identified as "private" I have no problem using the set / append text method, but that prevents me from using the text area in other classes, which is what I need to do.

However, now when I try to execute either appendText () or setText () like:

feedBack.setText("Connection Made"); 

      

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


EDIT, check / smash proof with a tiny program. I didn't make another class for this, since the textbox won't work in the control.

FXML:

<?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="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="textareatester.FXMLDocumentController">
<children>
<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" />
</children>
</AnchorPane>

      

Basic 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 {

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

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
}

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

      

Controller class:

package textareatester;

import java.net.URL;
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 {

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

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

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

      

Solution for those interested: I ended up just returning my result strings from my methods and then adding them to the TextArea.

+3


source to share


1 answer


The problem lies in the static

definition TextArea

. Because the attributes are static

bound to a class and not an object, the feedback is never triggered and matters null

when accessed in the EventHandler.



+1


source







All Articles