Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更新TreeTableView时出现看似随机的NullPointerException_Java_Javafx_Treetableview - Fatal编程技术网

Java 更新TreeTableView时出现看似随机的NullPointerException

Java 更新TreeTableView时出现看似随机的NullPointerException,java,javafx,treetableview,Java,Javafx,Treetableview,我刚刚将过滤添加到我的TreeTableView中,一个bug开始出现,看起来是随机的(可能不是随机的)。我可以通过滥发过滤文本字段来更新TreeTableView,直到阻塞为止。我得到以下堆栈跟踪,TreeTableView不再更新,但程序不会崩溃。与我的代码相关的堆栈跟踪的唯一部分指向cellFactory,我在其中重写CheckBoxTreeTableCell的updateItem,以实现顶层行的样式: colActShop.setCellFactory(column ->{

我刚刚将过滤添加到我的TreeTableView中,一个bug开始出现,看起来是随机的(可能不是随机的)。我可以通过滥发过滤文本字段来更新TreeTableView,直到阻塞为止。我得到以下堆栈跟踪,TreeTableView不再更新,但程序不会崩溃。与我的代码相关的堆栈跟踪的唯一部分指向cellFactory,我在其中重写CheckBoxTreeTableCell的updateItem,以实现顶层行的样式:

colActShop.setCellFactory(column ->{
        return new CheckBoxTreeTableCell<Product, Boolean>(){
            @Override
            public void updateItem(Boolean item, boolean empty) {
                super.updateItem(item, empty);
                boolean isTopLevel = getTreeTableView().getRoot().getChildren()
                                   .contains(getTreeTableRow().getTreeItem());
                if(item == null || empty){
                    setText(null);
                    setGraphic(null);
                    //It crashes on the following line according to the stack trace
                    getTreeTableRow().getStyleClass().remove("topLevelRow");
                }else{
                    if(isTopLevel){
                        getTreeTableRow().getStyleClass().add("topLevelRow");
                    }else{
                        getTreeTableRow().getStyleClass().remove("topLevelRow");
                    }

                    setEditable(!isTopLevel);   
                }
            }
        };
    });
它似乎与已打开3次并关闭但未发表任何评论的相同

堆栈跟踪:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at com.sun.javafx.scene.control.skin.CellSkinBase.access$200(CellSkinBase.java:54)
    at com.sun.javafx.scene.control.skin.CellSkinBase$StyleableProperties$1.getStyleableProperty(CellSkinBase.java:149)
    at com.sun.javafx.scene.control.skin.CellSkinBase$StyleableProperties$1.getStyleableProperty(CellSkinBase.java:138)
    at javafx.scene.CssStyleHelper.resetToInitialValues(CssStyleHelper.java:447)
    at javafx.scene.CssStyleHelper.createStyleHelper(CssStyleHelper.java:180)
    at javafx.scene.Node.reapplyCss(Node.java:8983)
    at javafx.scene.Node.reapplyCss(Node.java:9012)
    at javafx.scene.Node.impl_reapplyCSS(Node.java:8946)
    at javafx.scene.Node$3.onChanged(Node.java:1023)
    at com.sun.javafx.collections.TrackableObservableList.lambda$new$29(TrackableObservableList.java:45)
    at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(ListListenerHelper.java:164)
    at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
    at javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:233)
    at javafx.collections.ListChangeBuilder.commit(ListChangeBuilder.java:482)
    at javafx.collections.ListChangeBuilder.endChange(ListChangeBuilder.java:541)
    at javafx.collections.ObservableListBase.endChange(ObservableListBase.java:205)
    at javafx.collections.ModifiableObservableListBase.remove(ModifiableObservableListBase.java:183)
    at javafx.collections.ModifiableObservableListBase.remove(ModifiableObservableListBase.java:171)
    at homier.farmGame.controller.Engine$2.updateItem(Engine.java:768)
    at homier.farmGame.controller.Engine$2.updateItem(Engine.java:1)
    at javafx.scene.control.TreeTableCell.updateItem(TreeTableCell.java:630)
    at javafx.scene.control.TreeTableCell.indexChanged(TreeTableCell.java:457)
    at javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:116)
    at com.sun.javafx.scene.control.skin.TableRowSkinBase.recreateCells(TableRowSkinBase.java:671)
    at com.sun.javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:499)
    at com.sun.javafx.scene.control.skin.TreeTableRowSkin.updateCells(TreeTableRowSkin.java:220)
    at com.sun.javafx.scene.control.skin.TableRowSkinBase.checkState(TableRowSkinBase.java:631)
    at com.sun.javafx.scene.control.skin.TableRowSkinBase.computePrefHeight(TableRowSkinBase.java:571)
    at javafx.scene.control.Control.computePrefHeight(Control.java:547)
    at javafx.scene.Parent.prefHeight(Parent.java:935)
    at javafx.scene.layout.Region.prefHeight(Region.java:1435)
    at com.sun.javafx.scene.control.skin.VirtualFlow.resizeCellSize(VirtualFlow.java:1947)
    at com.sun.javafx.scene.control.skin.VirtualFlow.addLeadingCells(VirtualFlow.java:1247)
    at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1194)
    at javafx.scene.Parent.layout(Parent.java:1087)
    at javafx.scene.Parent.layout(Parent.java:1093)
    at javafx.scene.Parent.layout(Parent.java:1093)
    at javafx.scene.Parent.layout(Parent.java:1093)
    at javafx.scene.Parent.layout(Parent.java:1093)
    at javafx.scene.Scene.doLayoutPass(Scene.java:552)
    at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2397)
    at com.sun.javafx.tk.Toolkit.lambda$runPulse$29(Toolkit.java:398)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:397)
    at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:424)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:518)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:498)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:491)
    at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$403(QuantumToolkit.java:319)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Unknown Source)
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at com.sun.javafx.scene.control.skin.CellSkinBase.access$200(CellSkinBase.java:54)
    at com.sun.javafx.scene.control.skin.CellSkinBase$StyleableProperties$1.getStyleableProperty(CellSkinBase.java:149)
    at com.sun.javafx.scene.control.skin.CellSkinBase$StyleableProperties$1.getStyleableProperty(CellSkinBase.java:138)
    at javafx.scene.CssStyleHelper.resetToInitialValues(CssStyleHelper.java:447)
    at javafx.scene.CssStyleHelper.createStyleHelper(CssStyleHelper.java:180)
    at javafx.scene.Node.reapplyCss(Node.java:8983)
    at javafx.scene.Node.impl_reapplyCSS(Node.java:8946)
    at javafx.scene.control.Control$1.invalidated(Control.java:300)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:111)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
    at javafx.css.StyleableObjectProperty.set(StyleableObjectProperty.java:82)
    at javafx.scene.control.Control$1.set(Control.java:237)
    at javafx.scene.control.Control$1.set(Control.java:220)
    at javafx.scene.control.Control.setSkin(Control.java:217)
    at com.sun.javafx.scene.control.skin.TableRowSkinBase.recreateCells(TableRowSkinBase.java:673)
    at com.sun.javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:499)
    at com.sun.javafx.scene.control.skin.TreeTableRowSkin.updateCells(TreeTableRowSkin.java:220)
    at com.sun.javafx.scene.control.skin.TableRowSkinBase.checkState(TableRowSkinBase.java:631)
    at com.sun.javafx.scene.control.skin.TableRowSkinBase.computePrefHeight(TableRowSkinBase.java:571)
    at javafx.scene.control.Control.computePrefHeight(Control.java:547)
    at javafx.scene.Parent.prefHeight(Parent.java:935)
    at javafx.scene.layout.Region.prefHeight(Region.java:1435)
    at com.sun.javafx.scene.control.skin.VirtualFlow.resizeCellSize(VirtualFlow.java:1947)
    at com.sun.javafx.scene.control.skin.VirtualFlow.addLeadingCells(VirtualFlow.java:1247)
    at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1194)
    at javafx.scene.Parent.layout(Parent.java:1087)
    at javafx.scene.Parent.layout(Parent.java:1093)
    at javafx.scene.Parent.layout(Parent.java:1093)
    at javafx.scene.Parent.layout(Parent.java:1093)
    at javafx.scene.Parent.layout(Parent.java:1093)
    at javafx.scene.Scene.doLayoutPass(Scene.java:552)
    at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2397)
    at com.sun.javafx.tk.Toolkit.lambda$runPulse$29(Toolkit.java:398)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:397)
    at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:424)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:518)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:498)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:491)
    at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$403(QuantumToolkit.java:319)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Unknown Source)
有人有什么想法来指导我的调试吗?我需要放弃某些行的样式吗

编辑1
经过进一步的测试,它不像我想象的那么随机。我在“过滤器”字段中输入一个字母,然后在它卡住、可复制之前将其删除34次。如果我使用一系列字母(而不是每次都使用同一个字母),它似乎比34早出现,比如29或31。因此,它让我觉得有些东西在每次迭代中都没有被清除,但我不知道是什么。

我仍然不知道这个错误的原因。我想我找到了一种方法,通过在TreeTableView上使用
setRowFactory
来设置特殊行的样式,而不是在列上使用
setCellFactory
getTreeTableRow()
。这看起来很奇怪,因为行是同一个对象,我只是以不同的方式访问它,但到目前为止,我还没有用这种方式出现错误

    tableShop.setRowFactory(table-> {
        return new TreeTableRow<Product>(){
            @Override
            public void updateItem(Product pers, boolean empty) {
                super.updateItem(pers, empty);      
                boolean isTopLevel = table.getRoot().getChildren().contains(treeItemProperty().get());
                if(pers==null||empty) {
                    setText(null);
                    setGraphic(null);
                    getStyleClass().remove("topLevelRow");
                }else {
                    if(isTopLevel) {
                        if(!getStyleClass().contains("topLevelRow")) {
                            getStyleClass().add("topLevelRow");
                        }
                    }else {
                        getStyleClass().remove("topLevelRow");
                    }
                }
            }
        };
    });
tableShop.setRowFactory(表->{
返回新的TreeTableRow(){
@凌驾
public void updateItem(产品编号,布尔值为空){
super.updateItem(pers,空);
boolean isTopLevel=table.getRoot().getChildren().contains(treeItemProperty().get());
if(pers==null | |空){
setText(空);
设置图形(空);
getStyleClass().remove(“topLevelRow”);
}否则{
如果(isTopLevel){
如果(!getStyleClass()包含(“topLevelRow”)){
getStyleClass().add(“topLevelRow”);
}
}否则{
getStyleClass().remove(“topLevelRow”);
}
}
}
};
});

您能否创建一个复制错误的脚本?这样我们也可以在自己的机器上调试。还有,你使用的是什么版本的JavaFX?@Slaw我正在试着让它运行起来,但我甚至不知道从哪里开始。如果我想办法解决这个问题,我可能会找到答案,但我会继续努力。我想我用的是最新的版本。在eclipse中,我有C:\Program Files\Java\jdk1.8.0\U 191\jreit从概念上讲,从单元格设置行样式是错误的——因此在一天结束时,您会遇到您要求的麻烦;)无论如何,我不明白你为什么要后退:几周前你就得到了使用划船工厂的建议……我很可能会后退、横向、向下,有时还会向前。我想如果我没记错的话,我只需要改变一个单元格的可编辑状态,所以我就用了一个单元格工厂。然后我决定访问该行以设置样式,因为它对我是可用的。我并不清楚这在概念上是错误的。谢谢你一如既往的帮助。我仍然认为我发现了一个有趣的bug。
    tableShop.setRowFactory(table-> {
        return new TreeTableRow<Product>(){
            @Override
            public void updateItem(Product pers, boolean empty) {
                super.updateItem(pers, empty);      
                boolean isTopLevel = table.getRoot().getChildren().contains(treeItemProperty().get());
                if(pers==null||empty) {
                    setText(null);
                    setGraphic(null);
                    getStyleClass().remove("topLevelRow");
                }else {
                    if(isTopLevel) {
                        if(!getStyleClass().contains("topLevelRow")) {
                            getStyleClass().add("topLevelRow");
                        }
                    }else {
                        getStyleClass().remove("topLevelRow");
                    }
                }
            }
        };
    });