hharrison / java3d-core

Fork of the javax.media.j3d package
GNU General Public License v2.0
85 stars 39 forks source link

scenegraph.io Problem with OrderedGroup #20

Closed psymagic closed 8 years ago

psymagic commented 8 years ago

When writing an OrderedGroup that has its index array set to null, j3d throws: java.lang.NullPointerException at com.sun.j3d.utils.scenegraph.io.state.javax.media.j3d.OrderedGroupState.writeObject(OrderedGroupState.java:63)

Not sure if this is a bug, as one can always set the array, but considering Null is a legit value regarding the official OrderedGroupState docs, it would be nice if OrderedGroupState could handle an empty index array.

psymagic commented 8 years ago

After investigating a little further, it really seems like someone forgot to implement io for the case of OrderedGroup.getChildIndexOrder() == null.

Just did a working fix with these lines in com.sun.j3d.utils.scenegraph.io.state.javax.media.j3d.OrderedGroupState:

@Override
public void writeObject(DataOutput out) throws IOException {
    super.writeObject(out);

    int[] childIndexOrder = ((OrderedGroup) node).getChildIndexOrder();

    if (childIndexOrder == null) {
        out.writeInt(0);
    } else {
        out.writeInt(childIndexOrder.length);
        for (int i = 0; i < childIndexOrder.length; i++) {
            out.writeInt(childIndexOrder[i]);
        }
    }
}

@Override
public void readObject(DataInput in) throws IOException {
    super.readObject(in);

    int length = in.readInt();
    int[] childIndexOrder;

    if (length == 0) {
        childIndexOrder = null;
    } else {
        childIndexOrder = new int[length];
        for (int i = 0; i < childIndexOrder.length; i++) {
            childIndexOrder[i] = in.readInt();
        }
    }
    ((OrderedGroup) node).setChildIndexOrder(childIndexOrder);
}

It might not be perfect thou.

psymagic commented 8 years ago

just noticed this belongs to java3d-utils instead of core. added my comments there.