JavaFx:TreeTableView奇怪的NullpointerException

时间:2019-04-05 07:58:26

标签: java javafx nullpointerexception javafx-8 treetableview

我注意到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。

enter image description here

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

作为一种解决方案,我将接受任何确实有效的解决方法,或者接受任何解释/ javafx错误报告,以表明他们正在使用该方法,或者在以下版本中进行了修复。

1 个答案:

答案 0 :(得分:3)

当您单击无列的空白区域时,最终将单击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