JavaFx:TreeTableView 奇怪的 NullpointerException
JavaFx: TreeTableView weird NullpointerException
我注意到 TreeTableView
中有一个奇怪的 NPE
如果您在没有行的 table 中双击,就会发生这种情况。我附上截图来说明我的意思。
我纯粹来自JavaFx 与我的代码无关。我也做了一个研究,没有找到任何与此相关的东西。
这是一个简单的重现代码:
控制器:
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="Whosebug.treeview.Controller">
<TreeTableView fx:id="treeTable">
<columns>
<TreeTableColumn fx:id="column" text="Test"/>
</columns>
</TreeTableView>
</AnchorPane>
如果您双击红色区域,则会抛出 NPE。
堆栈跟踪:
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.handle(BehaviorSkinBase.java:95)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.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.access00(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$handleMouseEvent4(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$null8(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Java版本:1.8u121
作为解决方案,我会接受任何真正有效的解决方法或任何 explanation/javafx 显示他们正在处理它或在以下版本中有修复的错误报告。
当您点击没有列的空白 space 时,您最终会点击 TreeTableRow
。显然,控件的行为 class 中存在错误,这意味着它无法处理包含 null
项的行。如果单击空的 space 列 ,则看不到此错误,因为单击 TreeTableCell
,其行为 class似乎没有遇到同样的问题。不知道具体区别在哪里,只是快速浏览了一下源码。
如@kleopatra 所述,停止抛出异常的一种方法是向 TreeTableRow
添加一个事件过滤器,该过滤器会消耗对空行的双击。
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;
});
注意:您需要 clickCount % 2 == 0
,因为行为 class 会在点击次数为偶数时尝试访问 TreeItem
。
我注意到 TreeTableView
如果您在没有行的 table 中双击,就会发生这种情况。我附上截图来说明我的意思。
我纯粹来自JavaFx 与我的代码无关。我也做了一个研究,没有找到任何与此相关的东西。
这是一个简单的重现代码:
控制器:
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="Whosebug.treeview.Controller">
<TreeTableView fx:id="treeTable">
<columns>
<TreeTableColumn fx:id="column" text="Test"/>
</columns>
</TreeTableView>
</AnchorPane>
如果您双击红色区域,则会抛出 NPE。
堆栈跟踪:
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.handle(BehaviorSkinBase.java:95)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.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.access00(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$handleMouseEvent4(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$null8(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Java版本:1.8u121
作为解决方案,我会接受任何真正有效的解决方法或任何 explanation/javafx 显示他们正在处理它或在以下版本中有修复的错误报告。
当您点击没有列的空白 space 时,您最终会点击 TreeTableRow
。显然,控件的行为 class 中存在错误,这意味着它无法处理包含 null
项的行。如果单击空的 space 列 ,则看不到此错误,因为单击 TreeTableCell
,其行为 class似乎没有遇到同样的问题。不知道具体区别在哪里,只是快速浏览了一下源码。
如@kleopatra 所述,停止抛出异常的一种方法是向 TreeTableRow
添加一个事件过滤器,该过滤器会消耗对空行的双击。
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;
});
注意:您需要 clickCount % 2 == 0
,因为行为 class 会在点击次数为偶数时尝试访问 TreeItem
。