JavaF
JavaFx在执行以下代码后
private void updateValues(IntegerProperty[][] items, int cols, int[][] newValues) {
IntStream.range(0, rows).parallel().forEach(row -> {
for (int col = 0; col < cols; col++) {
items[row][col].setValue(newValues[row][col] / 100);
}
});
}
会有以下报错
Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 7436 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266) at java.base/java.util.Objects.checkIndex(Objects.java:359) at java.base/java.util.ArrayList.get(ArrayList.java:427) at javafx.graphics/javafx.scene.Parent.updateCachedBounds(Parent.java:1704) at javafx.graphics/javafx.scene.Parent.recomputeBounds(Parent.java:1648) at javafx.graphics/javafx.scene.Parent.doComputeGeomBounds(Parent.java:1501) at javafx.graphics/javafx.scene.Parent$1.doComputeGeomBounds(Parent.java:115) at javafx.graphics/com.sun.javafx.scene.ParentHelper.computeGeomBoundsImpl(ParentHelper.java:84) at javafx.graphics/com.sun.javafx.scene.NodeHelper.computeGeomBounds(NodeHelper.java:116) at javafx.graphics/javafx.scene.Node.updateGeomBounds(Node.java:3813)
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot read field "boundsChanged" because "<local11>" is null
at javafx.graphics/javafx.scene.Parent.updateCachedBounds(Parent.java:1705)
at javafx.graphics/javafx.scene.Parent.recomputeBounds(Parent.java:1648)
at javafx.graphics/javafx.scene.Parent.doComputeGeomBounds(Parent.java:1501)
at javafx.graphics/javafx.scene.Parent$1.doComputeGeomBounds(Parent.java:115)
at javafx.graphics/com.sun.javafx.scene.ParentHelper.computeGeomBoundsImpl(ParentHelper.java:84)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.computeGeomBounds(NodeHelper.java:116)
at javafx.graphics/javafx.scene.Node.updateGeomBounds(Node.java:3813)
at javafx.graphics/javafx.scene.Node.getGeomBounds(Node.java:3775)
at javafx.graphics/javafx.scene.Node.getLocalBounds(Node.java:3723)
at javafx.graphics/javafx.scene.Node.updateTxBounds(Node.java:3877)
at javafx.graphics/javafx.scene.Node.getTransformedBounds(Node.java:3669)
at javafx.graphics/javafx.scene.Node.updateBounds(Node.java:771)
at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1835)
at javafx.graphics/javafx.scene.SubScene.updateBounds(SubScene.java:710)
at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
IntegerProperty[][] items 是Box的高度绑定的数组,执行本方法后,Box会自动更新高度,然后代码也是在UI线程中执行的,报错中也看不到任何自定义代码
问题就出在parallel方法上,parallel 是把流转换成一个并行流,本意是为了提升速度的,但用在此处就不合适了,改成普通的循环就好了
IntStream.range(0, rows).forEach(row -> {
for (int col = 0; col < cols; col++) {
items[row][col].setValue(newValues[row][col] / 100);
}
});