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?

2
  • 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". Commented Jun 13, 2016 at 10:38
  • This explained it well and simple, thanks Commented Jun 13, 2016 at 10:38

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.