java - ArrayList IndexOutOfBoundsException -


i'm trying create program draws shape specified number of sides keep getting errors , i'm not sure 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 half interior angle of 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 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 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 calculate coordinates of point on shape          }             }         } 

i'm trying odd number sided shapes work , output when testing nodes being equal 5:

136.55176280263456 exception in thread "awt-eventqueue-0" java.lang.indexoutofboundsexception: index: 1, size: 0 93.97368876500715 116.15786740995709     @ java.util.arraylist.rangecheckforadd(arraylist.java:612)     @ java.util.arraylist.add(arraylist.java:426)     @ graphs.shape.drawshape(shape.java:58)     @ graphs.shape.jbutton1actionperformed(shape.java:189)     @ graphs.shape.access$100(shape.java:10)     @ graphs.shape$2.actionperformed(shape.java:140)     @ javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:2018)     @ javax.swing.abstractbutton$handler.actionperformed(abstractbutton.java:2341)     @ javax.swing.defaultbuttonmodel.fireactionperformed(defaultbuttonmodel.java:402)     @ javax.swing.defaultbuttonmodel.setpressed(defaultbuttonmodel.java:259)     @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(basicbuttonlistener.java:252)     @ java.awt.component.processmouseevent(component.java:6505)     @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3321)     @ java.awt.component.processevent(component.java:6270)     @ java.awt.container.processevent(container.java:2229)     @ java.awt.component.dispatcheventimpl(component.java:4861)     @ java.awt.container.dispatcheventimpl(container.java:2287)     @ java.awt.component.dispatchevent(component.java:4687)     @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4832)     @ java.awt.lightweightdispatcher.processmouseevent(container.java:4492)     @ java.awt.lightweightdispatcher.dispatchevent(container.java:4422)     @ java.awt.container.dispatcheventimpl(container.java:2273)     @ java.awt.window.dispatcheventimpl(window.java:2719)     @ java.awt.component.dispatchevent(component.java:4687)     @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:723)     @ java.awt.eventqueue.access$200(eventqueue.java:103)     @ java.awt.eventqueue$3.run(eventqueue.java:682)     @ java.awt.eventqueue$3.run(eventqueue.java:680)     @ java.security.accesscontroller.doprivileged(native method)     @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76)     @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:87)     @ java.awt.eventqueue$4.run(eventqueue.java:696)     @ java.awt.eventqueue$4.run(eventqueue.java:694)     @ java.security.accesscontroller.doprivileged(native method)     @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76)     @ java.awt.eventqueue.dispatchevent(eventqueue.java:693)     @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:244)     @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:163)     @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:151)     @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:147)     @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:139)     @ java.awt.eventdispatchthread.run(eventdispatchthread.java:97) 

you using add insert object @ index 1, there isn't in arraylist yet, can't insert there. 1 greater size, 0, indexoutofboundsexception.

i'm not sure line 58, 1 of these lines:

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

i'm not sure why need insert @ point; add 1 argument append end of list , should work fine.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -