[英]JavaFx: TreeTableView weird NullpointerException
我注意到TreeTableView
有一個奇怪的NPE
如果您在沒有行的表中雙擊,就會發生這種情況。 附上一張截圖,以顯示我的確切意思。
我純粹來自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="stackoverflow.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$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
,其行為類似乎沒有遇到相同的問題。 不知道確切的區別在哪里,但是我只快速瀏覽了一下源代碼。
如@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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.