-2

I have a TextField in which there is some text but I want to change that text on some event but I am getting the NullPointerException.

I am using setText() method but still its not working. I am calling that function from other class .

Any Help?

Thanks in advance.

Kazekage Gaara
  • 14,764
  • 14
  • 55
  • 105
Bipin Bhandari
  • 662
  • 4
  • 9
  • 22
  • if you get NPE when calling setText on a textfield, then that textfield probably does not exist or you misspelled it, hence the NPE. Please post relevant code and im sure we can help you with this task – John Snow May 15 '12 at 13:30
  • solved it but thanks fr reply bro :) – Bipin Bhandari May 16 '12 at 05:54

2 Answers2

6

At beginning of the controller's class definition:

@FXML private TextField txtDescription;

Within the initialize method, add:

txtDescription = new TextField();

Within a method that acts on that text field, something like:

txtDescription.setText("This is my new text.");
Josh Darnell
  • 11,096
  • 9
  • 36
  • 63
Mike Baran
  • 71
  • 4
0

Make sure that you have a TextField definition in your .fxml file with the following:

 fx:id="myCoolTextField"

If you don't have that, init the text field in your display() method with the following:

myCoolTextField = new TextField();

You may also override the special initialize() method. This method is being called every time your scene updated.

@FXML
public void initialize() {
    myCoolTextField.setText("Here is my updated text.");
}
gokcand
  • 6,200
  • 2
  • 20
  • 37