Skip to content
Advertisement

javafx.fxml.LoadException when trying to display a list of objects in a TableView JavaFX

I am trying to display a list of objects in a JavaFX table but I keep running into a JavaFX exception when looping through the objects to add to the table, does anybody know what might be causing this, I’ve tried several different implementations of ObservableLists but they all see to produce the same result, my conclusion is that there could be an error with the class or the .fxml file. Thanks in advance.

JavaFX:

JavaScript

The Controller:

JavaScript

Goal Class:

JavaScript

Edit:

I’ve done some more debugging and it seems when I surround it with a try-catch block it’s pointing to my user object from the base controller being null, even though I believe I pass it to the controller here’s the base controller which holds the user object, and the call I use to pass the user object (confirmed the user is fine before the passing as a parameter). The only stack trace I seem to get is the loader one and this from a try catch:java.lang.NullPointerException: Cannot invoke "User.getGoals()" because "this.user" is null :

JavaScript

From the previous controller that passes the user object:

JavaScript

Thanks!

Edit II: Full stack trace:

JavaScript

Advertisement

Answer

The controller’s initialize() method is called during the execution of FXMLLoader.load(), so initialize(), and consequently getGoals() are called before you call setUser(...). So user is null at the time you try to call user.getGoals().

To fix this within the structure you have set up, you need to populate the table list when setUser(...) is called. You could do this with an overridden setUser(...) method in your subclass.

Also note it’s always a mistake to initialize @FXML-annotated fields (because they are initialized by the FXMLLoader to the elements defined in the FXML file). So your controller should look something like this:

JavaScript

A more standard approach would be to use a MVC-type design, where the user is represented by an ObjectProperty<User> and you observe it for changes. E.g.

JavaScript

and

JavaScript

Typically here the user would not be the only value you would need to observe, so it would be factored out into a model somewhere. See, for example, Applying MVC With JavaFx

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement