I have noticed a weird NPE in the TreeTableView
It happens if you doubleClick in the table where there is no row present. Ill attach a screenshot to show what I mean exactly.
I comes purely from JavaFx it has nothing to do with my code. I also did a research and didn't find anything related to this.
Here is a simple code to reproduce it:
Controller:
public class Controller implements Initializable {
@FXML
private TreeTableColumn<Model, String> column;
@FXML
private TreeTableView<Model> treeTable;
@Override
public void initialize(URL location, ResourceBundle resources) {
TreeItem<Model> root = new TreeItem<>(new Model("Root"));
treeTable.setRoot(root);
column.setCellValueFactory(value -> value.getValue().getValue().textProperty());
}
private class Model {
private StringProperty text;
Model(String text) {
this.text = new SimpleStringProperty(text);
}
public StringProperty textProperty() {
return text;
}
public String getText() {
return text.get();
}
@Override
public String toString() {
return getText();
}
}
}
.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TreeTableColumn?>
<?import javafx.scene.control.TreeTableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="stackoverflow.treeview.Controller">
<TreeTableView fx:id="treeTable">
<columns>
<TreeTableColumn fx:id="column" text="Test"/>
</columns>
</TreeTableView>
</AnchorPane>
If you double click in the red region a NPE is thrown.
Stacktrace:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.javafx.scene.control.behavior.TreeTableRowBehavior.handleClicks(TreeTableRowBehavior.java:89)
at com.sun.javafx.scene.control.behavior.CellBehaviorBase.simpleSelect(CellBehaviorBase.java:259)
at com.sun.javafx.scene.control.behavior.TableRowBehaviorBase.doSelect(TableRowBehaviorBase.java:120)
at com.sun.javafx.scene.control.behavior.CellBehaviorBase.mousePressed(CellBehaviorBase.java:150)
at com.sun.javafx.scene.control.behavior.TableRowBehaviorBase.mousePressed(TableRowBehaviorBase.java:64)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:95)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Java version: 1.8u121
As a solution I would accept any workaround which really works or any explanation/javafx bug-report which shows they are working on it or there is a fix in a following version.
When you click in the empty space, where there's no columns, you end up clicking on a TreeTableRow
. Apparently there is a bug in the control's behavior class that means it can't handle rows with null
items. You don't see this bug if you click in the empty space of a column because that clicks on a TreeTableCell
, whose behavior class does not seem to suffer from the same problem. Not sure where the exact difference is, but I only quickly glanced at the source code.
One way to stop the exception being thrown, as @kleopatra mentioned, is to add an event filter to the TreeTableRow
that consumes double clicks on empty rows.
var table = new TreeTableView<String>();
table.setRowFactory(ttv -> {
var row = new TreeTableRow<String>();
row.addEventFilter(MouseEvent.MOUSE_PRESSED, event -> {
if (row.getItem() == null && event.getClickCount() % 2 == 0) {
event.consume();
}
});
return row;
});
Note: You need clickCount % 2 == 0
because the behavior class will try to access the TreeItem
when the click count is even.