删除任何 HBox 子节点会在 JAVAFX 中引发 NullPointerException

Removing any HBox child nodes throws a NullPointerException in JAVAFX

在此设计中,当我尝试从作为 HBox 的父节点删除 VBox 时,我看到 VBox 节点已从场景中删除,但是,它在删除元素后抛出 NullPointerException。

非常感谢任何帮助...

这是我的带有选项卡的主控制器

public class WorkspaceController {
    private HBox container;
    private ScrollPane scrollPane;

    public WorkspaceController(Stage stage, Project project) {
        this.stage = stage;
        this.project = project;
    }

    @FXML
    public void initialize() { setupTabs(); }

    private void setupTabs() {
        Tab tab = new Tab(project.getName());
        container = new HBox(10);
        scrollPane = new ScrollPane();
        for (Column column : project.getColumns()) {
            createColumns(tab, column, container);
        }
        tabPane.getTabs().add(tab);
    }
    public void createColumns(Tab tab, Column column, HBox container) {
        ColumnController col = new ColumnController(column, this);
        container.getChildren().add(col);
        scrollPane.setContent(container);
        tab.setContent(sp);
    }
    public void deleteColumn(VBox vbox) {
        Tab tab = tabPane.getSelectionModel().getSelectedItem();
        sp = (ScrollPane) tab.getContent();
        container = (HBox) sp.getContent();
        container.getChildren().remove(vbox);
    }
}

这是我的扩展 VBox 的 ColumnController

public class ColumnController extends VBox {

    public ColumnController(Column column, WorkspaceController parentController) {
        this.column = column;
        this.parent = parentController;
        
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/ColumnView.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }

    @FXML
    public void initialize() {
        columnName.setText(column.getName());
        this.setId(column.getName());
        deleteColumn.setOnAction(e -> {
            this.parent.deleteColumn(this);
        });
    }
}

堆栈跟踪

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at java.base/java.util.Objects.requireNonNull(Objects.java:208)
    at javafx.controls/javafx.scene.control.skin.MenuButtonSkinBase.lambda$new(MenuButtonSkinBase.java:206)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:447)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:446)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)

我解决了这个问题...这是 JavaFX 版本 17 中的一个错误。恢复到版本 11 解决了这个问题