Skip to content
Advertisement

NullPointerException, even though object is pointing to the FXML instance

(I am using Scene Builder…)

Here is the MusicGeneratorGUI class…

JavaScript

Here I have instantiated the object from the FXML file with the same ID…

JavaScript

However, System.out.println(icon); in the main method of the controller class produces null.

The ‘icon’ Sphere object is null.

Here is the main class… (‘controller class’)

JavaScript

Advertisement

Answer

You need to create an instance of your loader.

JavaScript

Then you can get access to an instance of your controller.

JavaScript

Then you can access the icon from the controller.

JavaScript

Note, you can still get the parent via.

JavaScript

You might want to consider doing this from the initialize method of the controller, then you don’t need to expose the icon of the Controller. Also, why is your icon static? I think you need to ditch the static.

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