0

github link: https://github.com/AEkman/QuizApplication/tree/master/src

I have a project with FXML gui.

First i load my label in ClientController.java and make a set method:

    @FXML
    private TextArea textAreaConsole;

    public void setTextAreaConsole(String text){
        textAreaConsole.appendText(text);
    }

In my other class ChatClient.java i want to send a text in variable message to textAreaConsole via the set method.

ClientController clientController = new ClientController();

clientController.setTextAreaConsole(message);

But it seems like i can't get the textAreaConsole to be initialized. I only get nullpointerexception. What am I missing? How do i initialize my label to be able to be changed via the set method, or is there a better solution?

I've tried loading the fxml file and import label again without success. I've tried instantiate the Controller in constructor without success.

castlesnake
  • 37
  • 1
  • 11
  • Your `TextArea` is annoted as `@FXML`, which means that the `FXMLLoader` will inject this field on loading the FXML file. If you instantiate the controller like you did, it will be not automagically initialized of course. – DVarga Apr 05 '17 at 10:23
  • I tried initialize the loader in my second class aswell but with the same nullpointer exception. I don't know how to initialize it so I can use my setter method. – castlesnake Apr 05 '17 at 10:41
  • You need to call `setTextAreaConsole` on the actual controller. You are calling it on an object you create that just happens to be the same class as the controller. You can get the controller from the `FXMLLoader` when you load the FXML. – James_D Apr 05 '17 at 11:42
  • And how would I go on about getting the loader? – castlesnake Apr 05 '17 at 12:13

0 Answers0