title: How to correct Arraylist index out of bounds for length error in Java
body: <p>I am writing a program for school which acts as a cash register. I am asking for input for prices of items and playing them into an ongoing ArrayList until the user enters -1 or 0. 0 is to re-enter the previous price in case of mistake and -1 terminates the loop.</p>
  
  <p>I am getting the </p>
  
  <blockquote>
    <p>java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0</p>
  </blockquote>
  
  <p>error when I try to run my code. 
  I have to include a method called <code>removeLastEntry()</code>, which will remove the last price entered into the array upon a 0 being entered by the user. How can I ensure the array is populated and I am indeed removing the last entry?</p>
  
  <p>I am running Java 11 in Eclipse.</p>
  
  <p>The code runs fine without the method being used, as I reduce my counter and in the next iteration of the loop, the previous array location is overwritten, regardless whether it has been removed or not. The method itself is set to remove ArrayList.size()-1 so that it removes the last entry. I have tried this with -2, and 0, and it still runs out of bounds.</p>
  
  <p>I read through previous questions and many people had not populated the array.
  So I ran a print stub to make sure that the ArrayList has been properly populated, and it has: when two items were placed into the ArrayList size was equal to 2. The error code also goes up the more items I place into the code but is always items - 1 index out of bounds at items - 1 length 
  I'm sure I'm making a rookie mistake but I'm unable to find it and it is driving me insane!</p>
  
  <p>for full error context:</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
        at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
        at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
        at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
        at java.base/java.util.Objects.checkIndex(Objects.java:372)
        at java.base/java.util.ArrayList.get(ArrayList.java:458)
        at C_M_iDeaProject.main(C_M_iDeaProject.java:76)</p>
  </blockquote>
  
  <pre><code>// declare our array list which will hold our prices!
  ArrayList&lt;Double&gt; prices = new ArrayList&lt;Double&gt;();
  
  
  // declaring variables to terminate loop, count prices, total prices, and current entry for conditions
          int counter  = 0;
          double entry = 0;
          double total = 0;
  
  // our loop to continuously add prices to the array list from input
  while (entry != -1) {
  
      System.out.println("Enter a price for item #" + (counter+1) + ": ");
      entry = myInput.nextDouble();
  
  // if the entry is a price we will add it to prices and continue with the loop
  
      if (entry != 0 &amp;&amp; entry != -1) {
              prices.add(entry);
              total += entry;
              counter++;
      }
  
  //if the entry is 0 we will revert back to the previous iteration to re-add
  
      else if (entry == 0.0) {
          total -= prices.get(counter-1);
          removeLastEntry(prices);
          counter--;
      }
  
  public static void removeLastEntry(ArrayList&lt;Double&gt; anArrayList) {
      anArrayList.remove(anArrayList.size()-1);
  }
  </code></pre>