0

So I am new to Java FX and I have a textfield element which I want to disable editing on. I can't change it to just text as I need a field later on. Besides, I really want to find out why this is happening.

This is how my constructor and variable declaration looks like:

   @FXML
    private TextField display;

public Controller(){
    display.setEditable(false);
    display.setMouseTransparent(true);
    display.setFocusTraversable(false);

}

This is my FXML:

      <TextField fx:id="display" id="displayCSS"></TextField>

Everytime I launch the application, I get a nullpointer exception beacuse I can't use any methods on a null object. Fine. So I moved the constructor code to a clicklistener for another button and that worked just fine. So I can use the display reference any time I want, except in the constructor. Is there something about constructors and Java fx that I don't understand? Why is this happening?

Essej
  • 892
  • 6
  • 18
  • 33
  • Please take a look on the linked question. Move the code from the constructor to the `initialize()` method as "it will be called once on an implementing controller when the contents of its associated document have been completely loaded". – DVarga Jun 13 '16 at 10:38
  • This explained it well and simple, thanks – Essej Jun 13 '16 at 10:38

0 Answers0