0

Basically whenever I press Run in my java IDE (Eclipse) it outputs:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 36 out of bounds for length 36
at javax.media.j3d.GeometryArrayRetained.setCoordinate(GeometryArrayRetained.java:3634)
at javax.media.j3d.GeometryArray.setCoordinate(GeometryArray.java:1336)
at Main.Shapes.Square.Make(Square.java:38)
at Main.Second.<init>(Second.java:24)
at Main.Main.main(Main.java:7)

the class it points out is called Square this is what it contains:

package Main.Shapes;

import javax.media.j3d.Node;
import javax.media.j3d.QuadArray;
import javax.media.j3d.Shape3D;
import javax.vecmath.Point3f;

public class Square {
    Node shape;
    
    public Square() {
        
    }
    
    public Node Make(float Size) {
        QuadArray polygon1 = new QuadArray (12, QuadArray.COORDINATES);
        
        //Front
        polygon1.setCoordinate(0, new Point3f (0f, 0f, 0f));
        polygon1.setCoordinate(1, new Point3f (Size, 0f, 0f));
        polygon1.setCoordinate(2, new Point3f (Size, Size, 0f));
        polygon1.setCoordinate(3, new Point3f (0f, Size, 0f));

        //Back
        polygon1.setCoordinate(4, new Point3f (0f, 0f, -Size));
        polygon1.setCoordinate(5, new Point3f (Size, 0f, -Size));
        polygon1.setCoordinate(6, new Point3f (Size, Size, -Size));
        polygon1.setCoordinate(7, new Point3f (0f, Size, -Size));

        //Right
        polygon1.setCoordinate(8, new Point3f (Size, 0f, 0f));
        polygon1.setCoordinate(9, new Point3f (Size, Size, 0f));
        polygon1.setCoordinate(10, new Point3f(Size, Size, -Size));
        polygon1.setCoordinate(11, new Point3f(Size, 0f, -Size));
        
        //left
        polygon1.setCoordinate(11, new Point3f (0f, 0f, 0f));
        polygon1.setCoordinate(12, new Point3f (0f, Size, 0f));
        polygon1.setCoordinate(13, new Point3f(0f, Size, -Size));
        polygon1.setCoordinate(14, new Point3f(0f, 0f, -Size));

        shape = new Shape3D(polygon1);
        return shape;
    }
}

the lines it points out are:

        polygon1.setCoordinate(12, new Point3f (0f, Size, 0f));
        polygon1.setCoordinate(13, new Point3f(0f, Size, -Size));
        polygon1.setCoordinate(14, new Point3f(0f, 0f, -Size));

and when deleted the program runs fine but I cant delete them since they are used to create a face of a cube.

Anybody got any ideas?

E_net4
  • 27,810
  • 13
  • 101
  • 139
Hoax
  • 76
  • 8
  • 1
    Java indexes are 0 based. If your Array is of size 36, then the indexes are from 0 - 35. Maybe because you are using a 3D API the indexes should be between 0 - 11? – camickr Jul 19 '21 at 14:48
  • 2
    Your `QuadArray` clearly has a size of 12 (ie. index 0-11). Yet you're setting 12+. – Tim Hunter Jul 19 '21 at 14:48
  • 1
    THANK YOU SO MUCH @TimHunter ! I haven't played around with this project for ages so i didnt know much about what i was doing and again, thank you! – Hoax Jul 19 '21 at 14:50
  • Also might want to double check your last group of 4 points, you're setting index 11 twice. – Tim Hunter Jul 19 '21 at 14:59
  • Does this answer your question? [How to avoid ArrayIndexOutOfBoundsException or IndexOutOfBoundsException?](https://stackoverflow.com/questions/32568261/how-to-avoid-arrayindexoutofboundsexception-or-indexoutofboundsexception) – E_net4 Jul 19 '21 at 15:05

1 Answers1

0

That's because almost every programming language starts counting on 0. So the last index would be Size-1.

Themathix
  • 56
  • 6