1

I keep getting a NullPointerException when trying to populate a ChoiceBox from an FXML file in JavaFX. I'm using SceneBuilder to create the FXML file then importing it into an IntelliJ project.

I've made sure that it's referencing the right class, I've even tried putting it into a separate class other than Main, any help would be much appreciated.

FXML Code:

<VBox prefHeight="600.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/10.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Main">

<ChoiceBox id="algorithmCB" fx:id="algorithmCB" layoutX="23.0" layoutY="41.0" prefHeight="27.0" prefWidth="99.0" />

Java Code:

public class Main extends Application {

@FXML
private ChoiceBox algorithmCB;

ObservableList<String> list = FXCollections.observableArrayList("item1", "item2", "item3");

@Override
public void start(Stage primaryStage) throws Exception{

    Parent root = FXMLLoader.load(getClass().getResource("DissUI.fxml"));
    primaryStage.setTitle("Algorithm Benchmark");
    primaryStage.setScene(new Scene(root, 900, 600));
    primaryStage.setResizable(false);
    primaryStage.show();


    algorithmCB.setItems(list);
}

public static void main(String[] args) {
    launch(args);
}
ca098
  • 68
  • 7

0 Answers0