JavaFX应用程序中出现意外的ArrayIndexOutOfBoundsException,请参考无数组 [英] Unexpected ArrayIndexOutOfBoundsException in JavaFX application, refering to no array

查看:172
本文介绍了JavaFX应用程序中出现意外的ArrayIndexOutOfBoundsException,请参考无数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

 public void setContent(Importer3D importer) {
    if (DEBUG) {
        System.out.println("Initialization of Mesh's arrays");
    }
    coords = importer.getCoords();
    texCoords = importer.getTexCoords();
    faces = importer.getFaces();
    if (DEBUG) {
        System.out.println("Applying Mesh's arrays");
    }
    mesh = new TriangleMesh();
    mesh.getPoints().setAll(coords);
    mesh.getTexCoords().setAll(texCoords);
    mesh.getFaces().setAll(faces);
    if (DEBUG) {
        System.out.println("Initialization of the material");
    }
    initMaterial();
    if (DEBUG) {
        System.out.println("Setting the MeshView");
    }
    meshView.setMesh(mesh);
    meshView.setMaterial(material);
    meshView.setDrawMode(DrawMode.FILL);
    if (DEBUG) {
        System.out.println("Adding to 3D scene");
    }
    root3d.getChildren().clear();
    root3d.getChildren().add(meshView);
    if (DEBUG) {
        System.out.println("3D model is ready!");
    }
}

Imporeter3D类部分:

The Imporeter3D class part:

  private void load(File file) {

    stlLoader = new STLLoader(file);
}

public float[] getCoords() {
    return stlLoader.getCoords();
}

public float[] getTexCoords() {
    return stlLoader.getTexCoords();
}

public int[] getFaces() {
    return stlLoader.getFaces();
}

STLLoader:

The STLLoader:

public class STLLoader{
public STLLoader(File file) {
    stlFile = new STLFile(file);
    loadManager = stlFile.loadManager;
    pointsArray = new PointsArray(stlFile);
    texCoordsArray = new TexCoordsArray();
}

public float[] getCoords() {
    return pointsArray.getPoints();
}

public float[] getTexCoords() {
    return texCoordsArray.getTexCoords();
}

public int[] getFaces() {
    return pointsArray.getFaces();
}

private STLFile stlFile;
private PointsArray pointsArray;
private TexCoordsArray texCoordsArray;
private FacesArray facesArray;
public  SimpleBooleanProperty finished = new SimpleBooleanProperty(false);
public LoadManager loadManager;}

PointsArray文件:

PointsArray file:

public class PointsArray {
public PointsArray(STLFile stlFile) {
    this.stlFile = stlFile;
    initPoints();
}

private void initPoints() {
    ArrayList<Double> pointsList = stlFile.getPoints();
    ArrayList<Double> uPointsList = new ArrayList<>();


    faces = new int[pointsList.size()*2];
    int n = 0;
    for (Double d : pointsList) {
        if (uPointsList.indexOf(d) == -1) {
            uPointsList.add(d);
        }
        faces[n] = uPointsList.indexOf(d);
        faces[++n] = 0;
        n++;
    }
    int i = 0;

    points = new float[uPointsList.size()];
    for (Double d : uPointsList) {
        points[i] = d.floatValue();
        i++;
    }
}

public float[] getPoints() {
    return points;
}

public  int[] getFaces() {
    return faces;
}

private float[] points;
private int[] faces;
private STLFile stlFile;
public static boolean DEBUG = true;

}

和STLFile: p>

And STLFile:

    ArrayList<Double> coords = new ArrayList<>();
double temp;
private void readV(STLParser parser) {
    for (int n = 0; n < 3; n++) {
        if(!(parser.ttype==STLParser.TT_WORD && parser.sval.equals("vertex"))) {
            System.err.println("Format Error:expecting 'vertex' on line " + parser.lineno());
        } else {
            if (parser.getNumber()) {
                temp = parser.nval;
                coords.add(temp);
                if(DEBUG) {
                    System.out.println("Vertex:");
                    System.out.print("X=" + temp + " ");
                }

                if (parser.getNumber()) {
                    temp = parser.nval;
                    coords.add(temp);
                    if(DEBUG) {
                        System.out.print("Y=" + temp + " ");
                    }

                    if (parser.getNumber()) {
                        temp = parser.nval;
                        coords.add(temp);
                        if(DEBUG) {
                            System.out.println("Z=" + temp + " ");
                        }


                        readEOL(parser);
                    } else System.err.println("Format Error: expecting coordinate on line " + parser.lineno());
                } else System.err.println("Format Error: expecting coordinate on line " + parser.lineno());
            } else System.err.println("Format Error: expecting coordinate on line " + parser.lineno());
        }
        if (n < 2) {
            try {
                parser.nextToken();
            } catch (IOException e) {
                System.err.println("IO Error on line " + parser.lineno() + ": " + e.getMessage());
            }
        }
    }

}

public ArrayList<Double> getPoints() {
    return coords;
}

由于所有这些代码,我希望得到3d模型MeshView。但是目前的结果是非常奇怪的:一切正常,在 DEBUG 模式我得到 3d模型准备好了 setContent(),然后意外 ArrayIndexOutOfBoundsException

As a result of all of this code, I expected to get 3d model in MeshView. But the present result is very strange: everything works and in DEBUG mode I get 3d model is ready! from setContent(), and then unexpected ArrayIndexOutOfBoundsException:

File readed
Initialization of Mesh's arrays
Applying Mesh's arrays
Initialization of the material
Setting the MeshView
Adding to 3D scene
3D model is ready!
java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 32252
    at com.sun.javafx.collections.ObservableFloatArrayImpl.rangeCheck(ObservableFloatArrayImpl.java:276)
    at com.sun.javafx.collections.ObservableFloatArrayImpl.get(ObservableFloatArrayImpl.java:184)
    at javafx.scene.shape.TriangleMesh.computeBounds(TriangleMesh.java:262)
    at javafx.scene.shape.MeshView.impl_computeGeomBounds(MeshView.java:151)
    at javafx.scene.Node.updateGeomBounds(Node.java:3497)
    at javafx.scene.Node.getGeomBounds(Node.java:3450)
    at javafx.scene.Node.getLocalBounds(Node.java:3432)
    at javafx.scene.Node.updateTxBounds(Node.java:3510)
    at javafx.scene.Node.getTransformedBounds(Node.java:3350)
    at javafx.scene.Node.updateBounds(Node.java:516)
    at javafx.scene.Parent.updateBounds(Parent.java:1668)
    at javafx.scene.SubScene.updateBounds(SubScene.java:556)
    at javafx.scene.Parent.updateBounds(Parent.java:1668)
    at javafx.scene.Parent.updateBounds(Parent.java:1668)
    at javafx.scene.Parent.updateBounds(Parent.java:1668)
    at javafx.scene.Parent.updateBounds(Parent.java:1668)
    at javafx.scene.Parent.updateBounds(Parent.java:1668)
    at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2309)
    at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:329)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:479)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:459)
    at com.sun.javafx.tk.quantum.QuantumToolkit$13.run(QuantumToolkit.java:326)
    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.access$300(WinApplication.java:39)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:101)
    at java.lang.Thread.run(Thread.java:724)
Exception in thread "JavaFX Application Thread" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 32252
    at com.sun.javafx.collections.ObservableFloatArrayImpl.rangeCheck(ObservableFloatArrayImpl.java:276)
    at com.sun.javafx.collections.ObservableFloatArrayImpl.get(ObservableFloatArrayImpl.java:184)

陌生人的事情是,这个堆栈在我关闭程序之前不会停止。而且它不指向我的任何阵列。这是什么?为什么会这样呢?

The stranger thing is that this stack doesn't stop until I close the program. And moreover it doesn't point to any my array. What is this? And why does it happen?

推荐答案

TriangleMesh将此数组视为点坐标(x,y,z,因为它是3D),因此总数组长度应为 cords.length%3 == 0

TriangleMesh treats this array as point coordinates (x,y,z since it is 3D), so total array length should be cords.length % 3 == 0.

这篇关于JavaFX应用程序中出现意外的ArrayIndexOutOfBoundsException,请参考无数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆