Scene Builder创建的ComboBox添加项时为空问题排查
问题:Scene Builder创建的Integer类型ComboBox初始化时触发NullPointerException
使用Scene Builder创建的简单整数类型ComboBox,尝试插入项或执行相关操作时,始终触发空指针异常,ComboBox实例为null。其他按钮及关闭功能均正常运行。
最小复现示例
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(); // 尝试操作时触发空指针,tracks为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; }
其他按钮和关闭功能都能正常工作,问题出在哪里?
更新
重装JavaFX包后程序正常运行,暂不清楚具体原因。
内容的提问来源于stack exchange,提问作者VMi
相关产品推荐
相关产品推荐

