title: How to fix this runtime error : java.lang.IndexOutOfBoundsException
body: <p>Here is my code. It compiles without errors. When executed, the runtime error is</p>
  
  <pre><code>java.lang.IndexOutOfBoundsException
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
  </code></pre>
  
  <p>The problematic lines are commented on. I'm not sure what is wrong, but I am new to <code>ArrayLists</code>.</p>
  
  <pre><code>import java.awt.geom.*;
  import java.util.Iterator;
  import java.util.ArrayList;
  import java.util.Scanner;
  import gpdraw.*;
  
  public class ArrayListTest {
      private ArrayList&lt;Point2D.Double&gt; myPolygon = 
              new ArrayList&lt;Point2D.Double&gt; (20);
      private Scanner scan = new Scanner(System.in);
  
      public ArrayListTest(){
          process();
          draw();
      }
  
      private void process(){
          double x, y;
          System.out.println("Enter a set of coordinate points that will"
                  + "be used to create an irregular polygon. ");
          System.out.println("Use this format: x y" 
                  + "\nEnter 911 to draw polygon.");
          while(1 &gt; 0){
              System.out.print("Enter a point: ");
              x = scan.nextDouble();
              if(x == 911) { break; }
              else{
                  y = scan.nextDouble();
                  Point2D.Double pt = new Point2D.Double(x, y);
                  addPt(pt);}
              }
          }
      }
  
      public void addPt(Point2D.Double pt){
          myPolygon.add(pt);
      }
  
      public void draw(){
          SketchPad paper = new SketchPad(400, 400);
          DrawingTool pen = new DrawingTool(paper);
          Iterator &lt;Point2D.Double&gt; iter = myPolygon.iterator();
          pen.up();
          double x  = myPolygon.get(0).getX();
          double y = myPolygon.get(0).getY();
          pen.move(x, y);
          pen.down(); 
          int i = 1; 
          while(iter.hasNext()){
              x = myPolygon.get(i).getX(); //The error is actually on this line,
              y = myPolygon.get(i).getY();
              pen.move(x, y);
              i++;
          }
          pen.move(myPolygon.get(0).getX(), myPolygon.get(0).getY());
      }
  
      public static void main(String[] args){
          ArrayListTest poly = new ArrayListTest();
      }
  }
  </code></pre>
  
  <p>The purpose of this program is to prompt the user for a set of points (on a coordinate plane), then draw out lines connecting those points, creating a irregular polygon. <br/> 
  Based on what was discussed, the error is thought to be triggered when the loop has gone through all of the other elements, and empty space is left. </p>
  
  <p>This causes the program to pull an object from something that is not there, so the <code>IndexOutOfBoundsException</code> occurs (I think).</p>