I am stuck in something basic.
A simple comboBox of integers, made from Scene Builder shows null when I try to insert items.
Min Rep Example
Fxml :
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<VBox prefHeight="500.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<children>
<AnchorPane prefHeight="500.0" prefWidth="650.0">
<children>
<TabPane layoutY="20.0" prefHeight="481.0" prefWidth="650.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="File Upload">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="322.0" prefWidth="654.0">
<children>
<Button fx:id="fileUpload" layoutX="249.0" layoutY="88.0" mnemonicParsing="false" prefHeight="31.0" prefWidth="139.0" text="Select file to upload" />
<Button fx:id="uploadBtn" layoutX="296.0" layoutY="132.0" mnemonicParsing="false" onAction="#onBtnClick" text="Upload" />
<Text fx:id="statusMsg" layoutX="285.0" layoutY="186.0" strokeType="OUTSIDE" strokeWidth="0.0" wrappingWidth="78.0" />
<Label layoutX="245.0" layoutY="204.0" text="Create Tracks in a file">
<font>
<Font size="18.0" />
</font>
</Label>
<ComboBox fx:id="tracks" layoutX="197.0" layoutY="247.0" onMouseClicked="#setTrackValue" prefHeight="25.0" prefWidth="89.0" promptText="Tracks" />
<Label layoutX="85.0" layoutY="251.0" prefHeight="17.0" prefWidth="103.0" text="Select No of tracks" />
<Button fx:id="generateTrackBtn" layoutX="354.0" layoutY="247.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="90.0" text="Generate" />
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="Multiwave UI">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</TabPane>
<Text fx:id="closeBtn" fontSmoothingType="LCD" layoutX="617.0" layoutY="16.0" onMouseClicked="#closeApp" strokeType="OUTSIDE" strokeWidth="0.0" text="X">
<font>
<Font name="System Bold" size="18.0" />
</font>
</Text>
</children>
</AnchorPane>
</children>
</VBox>
Controller:
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.text.Text;
public class Controller implements Initializable {
@FXML
private Button uploadBtn;
@FXML
private Text closeBtn;
@FXML
private ComboBox<Integer> tracks;
@FXML
public void onBtnClick(ActionEvent e) {
System.out.println("I am clicked");
}
@FXML
public void closeApp(Event e)
{
Stage s = (Stage) closeBtn.getScene().getWindow();
s.close();
}
@FXML
public void setTrackValue(ActionEvent e)
{
System.out.println("Combobox clicked");
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
tracks.getItems().clear(); //This is for trying, whatever I do it returns null
}
}
I tried inserting data, clearing items or even seperate action event, all the cases it returns null.
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.ComboBox.getItems()" because "this.tracks" is null
at Multitrack_Waveform_Viewer/application.Controller.initialize(Controller.java:55)
at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2655)
... 14 more
Main:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
System.out.println("Running");
launch(args);
}
@Override
public void start(Stage primaryStage) {
try {
System.out.println("Running");
Parent root = FXMLLoader.load(getClass().getResource("UI_WaveForm.fxml"));
primaryStage.setScene(new Scene(root,650,500));
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
}
Module info:
module Multitrack_Waveform_Viewer {
requires javafx.controls;
requires javafx.graphics;
requires javafx.fxml;
requires javafx.base;
opens application to javafx.graphics, javafx.fxml;
}
While the other button and close option works fine.
What am I doing wrong here ?
Update:
I reinstalled the JavaFX package and it ran fine. No idea why it happened