Issue
In this design, when I try and delete the VBox from the parent node which is an HBox, I see that the VBox node is removed from the scene, however, it throws a NullPointerException after deleting the element.
Would appreciate any helps...
this is my main controller that has tabs
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);
}
}
This is my ColumnController that extends VBox
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);
});
}
}
StackTrace
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$7(MenuButtonSkinBase.java:206)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Solution
I fixed the issue... it was a bug in Version 17 of JavaFX. reverting back to version 11 fixed the issue
Answered By - Tamim Daudzai