简体   繁体   中英

ArrayList IndexOutOfBoundsException

I'm trying to create a program which draws a shape with a specified number of sides but I keep getting errors and I'm not sure what the issue is.

public double tempx = 0;
    public double tempy = 0;
    public int Angle = 0;
    public double Length = 0;
    public int Nodes = 0;
    public int Height = 170;

    //public double xcoordinate = 0;
    //public double ycoordinate = 0;
    //Unused Variables

    /**
     * Creates new form Shape
     */
    public void drawShape(int Nodes,int Height){
        double Apothem=0;
        double Circumradius=0;
        double x = 180/Nodes;
        ArrayList<Double> xcoordinate = new ArrayList(Nodes);
        ArrayList<Double> ycoordinate = new ArrayList(Nodes);
        //Angle = (180*(Nodes-2))/2*Nodes;
        //Angle is half the interior angle of the shape(Currently Unused)
        Angle = 360/Nodes;


        if(Nodes%2==1){
            Length = (2*Height*Math.tan(Math.toRadians((x))))/(1+Math.cos(Math.toRadians((x))));
            Apothem = Length/(2*Math.tan(Math.toRadians((x))));
            Circumradius = Apothem/(Math.cos(Math.toRadians((180/Nodes))));
            System.out.println(Length);
            System.out.println(Apothem);
            System.out.println(Circumradius);
            //Calculating properties of an odd number of sides shape
        }
        if(Nodes%2==0){
            Length = Height*Math.tan(Math.toRadians((x)));
            Apothem = Length/(2*Math.tan(Math.toRadians((x))));
            //Calculating properties of an even number of sides shape
        }


        //double xcoordinate[] = new double[Nodes];
        //double ycoordinate[] = new double[Nodes];
        //Unused Variables

        if(Nodes%2 == 1){
                xcoordinate.add(1,Apothem);
                System.out.println(xcoordinate.get(1));
                ycoordinate.add(1,(double)0);
                for(int counter =2;counter<(Nodes+1);counter++){




                    xcoordinate.add((xcoordinate.get(counter-1)*Math.cos(Math.toRadians(Angle))-(ycoordinate.get(counter-1)*Math.sin(Math.toRadians(Angle)))));
                    ycoordinate.add((xcoordinate.get(counter-1)*Math.sin(Math.toRadians(Angle))+(ycoordinate.get(counter-1)*Math.cos(Math.toRadians(Angle)))));

                    //Uses Matrix Rotation to calculate all the coordinates of the point on the shape

        }
            }




    }

I'm currently trying to get odd number sided shapes to work and this is the output I get when testing with Nodes being equal to 5:

136.55176280263456
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
93.97368876500715
116.15786740995709
    at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:612)
    at java.util.ArrayList.add(ArrayList.java:426)
    at graphs.Shape.drawShape(Shape.java:58)
    at graphs.Shape.jButton1ActionPerformed(Shape.java:189)
    at graphs.Shape.access$100(Shape.java:10)
    at graphs.Shape$2.actionPerformed(Shape.java:140)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

You are using add to insert the object at index 1 , but there isn't anything in the ArrayList yet, so you can't insert it there. 1 is greater than the size, 0 , so you get an IndexOutOfBoundsException .

I'm not sure which is line 58, but probably one of these lines:

xcoordinate.add(1,Apothem);

ycoordinate.add(1,(double)0);

I'm not sure why you need to insert at that point; usually add with just one argument will append to the end of the list and that should work just fine.

you're trying to add at index 1 but your arraylist is empty to start-- there is no index 1.

Just use regular add. Or index 0.

Make this change:

            xcoordinate.add(0,Apothem);
            System.out.println(xcoordinate.get(0));
            ycoordinate.add(0,(double)0);

First position is 0 (not 1!!). Alternatively, you can use the normal add() method:

            xcoordinate.add(Apothem);
            System.out.println(xcoordinate.get(0));
            ycoordinate.add(double)0);

This may cause an issue in the future:

            for(int counter =2;counter<(Nodes+1);counter++){

Try setting counter = 1 (since ArrayList goes from 0 to Length-1)

I think the problem is here:

xcoordinate.add(1,Apothem);

You add this value at index 1 before you add anything at index 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM