0

i have a simple javafx FXML app that has a button and a textArea

I am trying to write to the textArea from another class (not the controller)

without sending the textArea to that class ,

i added a getter on my controller class, and on the writingClass i created an object of the ControllerClass

and then trying to write to the textArea , i am getting a java.lang.NullPointerException and java.lang.reflect.InvocationTargetException

what am i doing wrong ???

//Controller.java

package sample;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;

public class Controller {

    @FXML
    Button myButton;

    @FXML
    TextArea myTextArea;
    WriteToTextArea writeToTextArea;

    public TextArea getMyTextArea() {
        return myTextArea;
    }

    public void buttonPressed() {
        writeToTextArea = new WriteToTextArea();
        writeToTextArea.writeThis("trying To Write To TextArea");
    }
}

//WriteToTextArea.java

package sample;
import javafx.scene.control.TextArea;

public class WriteToTextArea {

    private Controller objectController;
    private TextArea textArea;
    public WriteToTextArea() {
        objectController = new Controller();
        textArea = new TextArea();
        textArea = objectController.getMyTextArea();
    }

    public void writeThis(String whatToWrite) {
        textArea.setText(whatToWrite);
    }
}
Wojtek
  • 1,390
  • 1
  • 19
  • 38
Dart
  • 47
  • 1
  • 6
  • The controller is an object typically created for you by the `FXMLLoader`. You are not calling `getMyTextArea()` on the controller, you are calling it on another object that happens to be of the same class. – James_D Feb 24 '17 at 11:04
  • ok so , how to access the getMyTextArea() on the controller? – Dart Feb 24 '17 at 11:16
  • Hmm: you're doing something slightly different here, by instantiating `WriteToTextArea` from the controller itself. Added an answer (though I assume you have figured it out by now anyway). – James_D Feb 24 '17 at 12:57

1 Answers1

1

The textArea is initialized in the controller by the FXMLLoader when the FXML file is loaded. It is only initialized in the controller, and won't be initialized in other instances of the same class (what would it be initialized to?). So when you create a new Controller instance with

 objectController = new Controller();

the textArea in that instance is null, so when you call

textArea.setText(whatToWrite);

you get a null pointer exception.

You need the WriteToTextArea instance to have a reference to the controller itself, not some arbitrary instance of the same class. You can do this by passing a reference to the controller to the WriteToTextArea constructor:

package sample;
import javafx.scene.control.TextArea;

public class WriteToTextArea {

    private Controller objectController;
    private TextArea textArea;

    public WriteToTextArea(Controller objectController) {
        this.objectController = objectController ;
        textArea = objectController.getMyTextArea();
    }

    public void writeThis(String whatToWrite) {
        textArea.setText(whatToWrite);
    }
}

and then in the controller code

public void buttonPressed() {
    writeToTextArea = new WriteToTextArea(this);
    writeToTextArea.writeThis("trying To Write To TextArea");
}
James_D
  • 201,275
  • 16
  • 291
  • 322