3

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"
wrapText="true"/>

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:

@FXML
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.

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>

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 {

@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 who are curious: I ended up simply returning my results strings from my methods and then appending them to the TextArea.

senex_subconscious
  • 221
  • 2
  • 6
  • 14
  • Have you loaded the fxml-File before you tryed to set the text? – Turing85 Nov 05 '14 at 17:18
  • If you mean the initialize method then yes that gets run before I attempt to do anything with the the textarea. – senex_subconscious Nov 05 '14 at 17:20
  • To load an fxml-file you normally use a FXMLLoader. This is, what i mean. Can you post the whole code and FXML-file in question? It looks like Java does not find the FXML-file. – Turing85 Nov 05 '14 at 17:24
  • Its a lot of code to post (the FXML is 200+ lines as it is and I've been discouraged from over sharing of non-essential code), but I will do what I can. EDIT, my main method is calling the FXML file with the FXMLLoader and the name is correct. I can load and run the program it just gives me the NPE when I later try to append text to the textarea. – senex_subconscious Nov 05 '14 at 17:26
  • Try to generate a minimum working example, chop the unnecessary parts =) – Turing85 Nov 05 '14 at 17:27
  • Okay world's tiniest tester/proof program coming for you! – senex_subconscious Nov 05 '14 at 17:40

1 Answers1

2

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

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • 2
    The key thing to point out is that this worked in earlier versions of JavaFX but was changed in JavaFX 8. See [this](http://stackoverflow.com/a/23109125/577417) post for more details. – Benjamin Gale Nov 05 '14 at 18:07
  • So what you're both saying is that I really can't do what I'm trying to do. It is better to pass Strings back to the controller and then to the text area to display them. Is that about right? – senex_subconscious Nov 05 '14 at 18:11
  • Apparently not (see http://stackoverflow.com/questions/23105433/javafx-8-compatibility-issues-fxml-static-fields). Why do you want to have the TextArea static? Maybe there is another solution for your problem? – Turing85 Nov 05 '14 at 18:20
  • I had made it static in an effort to make it work with the classes outside of the controller. – senex_subconscious Nov 05 '14 at 18:24
  • As explained in the post mentioned, you can get the Controller from the FXMLLoader. So this should not be an issue. – Turing85 Nov 05 '14 at 18:29
  • @Turing85 figured out what I have to do. Thanks for all your help both you! – senex_subconscious Nov 05 '14 at 18:30