1
votes

I am a beginner with JavaFX and I have a problem when I click the button, I am trying to setText() to TextArea from String. But, I got an error. This is my code:

@FXML
private TextArea chat1, chat2;             //text area show message

//in ActionEvent method of button
String s = this.message2.getText();
chat1.setText(Chat2.username2 + ": " + s);

Cannot invoke "javafx.scene.control.TextArea.setText(String)" because "this.chat1" is null

The error message already tells you chat1 is null. Obviously no-one can tell you why it's null from the code you have posted. - James_D
Did you load your .fxml file? Is chat1 an ID in that .fxml file? Your use of both chat2 and Chat2 in your question leads me to wonder if you aren’t paying attention to case in both places. - VGR
Create and post a minimal reproducible example, as previously requested. - James_D