0

I have main controller and an fxml file. In my main controller I have written:

@FXML
public static VBox spectrumListVBox;

And in my fxml file I have

<javafx.scene.layout.VBox fx:id="spectrumListVBox" prefHeight="324.0" prefWidth="191.0" />

In my main controller if I was trying to add multiple panes to this vbox and having errors. There are two things going on here. The first is that whilst running it would return a nullPointerException which at first I assumed was the pane I was adding to it was returning null. So I changed it to the following code. When I press a button I want it to add this new button to the vbox.

spectrumListVBox.getChrildren().add(new Button("Click Here");  

However this too presented with a nullPointerException, which leads me to the conclusion that the VBox isn't being initialised by the FXML file. If I change the code in the main controller to:

public static VBox spectrumListVBox = new VBox();

This results in no warnings when compiled, however I do not actually see anything added to my vbox. I can confirm everything else is loading correctly (my controller and fxml has about 50 elements which are all hooked up and working correctly). I have closely checked that the fx:id and the controller element name are matching, and I have tried cleaning the project in case anything else was lingering on.

samp17
  • 547
  • 1
  • 4
  • 16
  • 1
    You shouldn't be using class variables (`static`), but instance variables. The used FXML controller is an instance, not a class. – M. le Rutte Jun 24 '18 at 13:08
  • I cannot believe I overlooked this. Worked first time. It's like I keep looking for it to be something more complicated that I am not aware of rather than the basics. Thank you – samp17 Jun 24 '18 at 13:13

0 Answers0