gpt4 book ai didi

JavaFx:TreeTableView 奇怪的 NullpointerException

转载 作者:行者123 更新时间:2023-11-30 07:41:52 24 4
gpt4 key购买 nike

我注意到 TreeTableView 中有一个奇怪的 NPE

如果您在没有行的表格中双击,就会发生这种情况。我将附上屏幕截图以准确说明我的意思。

我纯粹来自 JavaFx 它与我的代码无关。我也做了一个研究,没有发现任何与此相关的东西。

这是一个简单的重现它的代码:

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>

如果您双击红色区域,则会抛出 NPE。

enter image description here

堆栈跟踪:

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版本:1.8u121

作为解决方案,我会接受任何真正有效的解决方法或任何说明/javafx 错误报告,表明他们正在处理它或在以下版本中有修复。

最佳答案

当您单击没有列的空白区域时,您最终会单击 TreeTableRow。显然,控件的行为类中存在一个错误,这意味着它无法处理包含 null 项的行。如果单击列的空白区域,则看不到此错误,因为单击的是 TreeTableCell,其行为类似乎没有遇到同样的问题.不知 Prop 体区别在哪里,只是快速浏览了一下源码。

如@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,因为当点击次数为偶数时,行为类将尝试访问 TreeItem

关于JavaFx:TreeTableView 奇怪的 NullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55530779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com