• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

NullPointerException calling setText after loading fxml

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has been over 5 years since I've worked with JavaFX. This seems like such a simple thing, yet I am not sure what I am missing. I am just trying to set the value of a text field, but I am getting a null pointer exception. It is being called after the loader, so I am not totally sure what I am missing.



Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.text.Text.setText(String)" because "this.txtHeader" is null
 
Marshal
Posts: 79978
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that the constructor? You seem to have declared txtHeader but neither given it private access, nor initialised the reference. A field declared and not initialised implicitly points to null.
 
Campbell Ritchie
Marshal
Posts: 79978
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Peremsky wrote:. . . because "this.txtHeader" is null

If only all exception messages would give such useful information!
 
Michael Peremsky
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought that by tying the fxid to the object in the fxml, it was supposed to create an instance of the object on the load.



Is this not the case? Again, it has been over 5 years.

If I created an object



would I then need to reassign it to the fxml object? How exactly?

 
Michael Peremsky
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Resolved. Not sure what was wrong, perhaps naming was off. But it is now loading the text properly.
 
Campbell Ritchie
Marshal
Posts: 79978
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I don't know enough FXML to help there.
 
Bartender
Posts: 303
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Peremsky wrote:I thought that by tying the fxid to the object in the fxml, it was supposed to create an instance of the object on the load.



FXMLLoader.load() does normally trigger @FXML field injection.

But you might want to use the initialize() method for initializing the field component rather than the constructor. Most of my controllers don't even have explict constructors. The initialize() method is safe to access FXML injected stuff from (implement Initializable). Actually I'd also suggest using fx:controller in the FXML rather than setController() which is a bit unorthodox IMO. It's more common for me to see it done something like this (I often put the FXMLLoader parts into a utility class):



Safe to access injected fields after initialize():


> Task :TestFxml.main()
Cons: null
initialize: VBox[id=testingVb]



Just make sure your controller implements Initializable.

Gluon Scene Builder will set the fx:controller attribute for you if you select it on the Controller area of the left panel. Rarely needed to mess with FXML directly. Some/all IDEs have FXML plugins also which will automatically generate and correct your @FXML injected stuff, helps eliminate simple mistakes.
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic