title: Getting a weird NoSuchElementException
body: <p>We are trying to compile our program, but we keep getting a <code>NoSuchElementException</code>. Anyone that has a clue on why this keeps occurring? Thanks in advance. In the following I will attach both the code where we implement the exception and also the main method.</p>
  
  <p>EDIT - whole code in the following:</p>
  
  <pre><code>import java.util.Iterator;
  import edu.princeton.cs.algs4.*;
  
  public class RandomQueue&lt;Item&gt; implements Iterable&lt;Item&gt; {
      private Item[] queue;
      private int N;
      private int size;
  
      // Your code goes here.
      public RandomQueue() { // create an empty random queue
          N = 0;
          size = 2;
          queue = (Item[]) new Object[size];
      }
  
      public boolean isEmpty() {// is it empty?
          if(N == 0) {
              return true;
          } else {
              return false;
          }
      }
  
      public int size() {// return the number of elements
          return size;
      }
  
      public void resizeArray() {
          if(3/4*size &lt; N) {
              size = size*2;
              Item[] queueUpdated = (Item[]) new Object[size];
              for(int i = 0; i &lt; queue.length; ++i) {
                  queueUpdated[i] = queue[i];
              }
              queue = queueUpdated;
          } else if (N &lt; 1/4*size) {
              size = size/2;
              Item[] queueUpdated = (Item[]) new Object[size];
              for(int i = 0; i &lt; size-1; ++i) {
                  queueUpdated[i] = queue[i];                 
              }
              queue = queueUpdated;
          }
  
      }
  
  
      public void enqueue(Item item) {// add an item
          if(N &lt; queue.length) {
              queue[N++] = item;
              resizeArray();
          }
      }
  
      public Item sample(){ // return (but do not remove) a random item
          if(isEmpty()) {
              throw new RuntimeException("No such elements");
          } else {
              return queue[StdRandom.uniform(N)];
          }
      }
  
      public Item dequeue(){ // remove and return a random item
          if(isEmpty()) {
              throw new RuntimeException("Queue is empty");
          } else {
              System.out.println(N);
              int indexFraArray = StdRandom.uniform(N);
              Item i = queue[indexFraArray];
              queue[N] = null;
              queue[indexFraArray] = queue[N--];
              resizeArray();
              return i;
          }
      }
  
      private class RandomQueueIterator&lt;E&gt; implements Iterator&lt;E&gt; {
          int i = 0;
          public boolean hasNext() {
              return i &lt; N;
          }
          public E next() {
              if (!hasNext()) {
                  throw new java.util.NoSuchElementException(); // line 88
              }
              i++;
              return (E) dequeue();
          }
          public void remove() {
              throw new java.lang.UnsupportedOperationException();
          }
      }
  
      public Iterator&lt;Item&gt; iterator() { // return an iterator over the items in 
          random order
          return new RandomQueueIterator();
      }
  
  
      // The main method below tests your implementation. Do not change it.
      public static void main(String args[]) {
          // Build a queue containing the Integers 1,2,...,6:
          RandomQueue&lt;Integer&gt; Q = new RandomQueue&lt;Integer&gt;();
          for (int i = 1; i &lt; 7; ++i) Q.enqueue(i); // autoboxing! cool!
  
          // Print 30 die rolls to standard output
          StdOut.print("Some die rolls: ");
          for (int i = 1; i &lt; 30; ++i) StdOut.print(Q.sample() +" ");
          StdOut.println();
  
          // Let's be more serious: do they really behave like die rolls?
          int[] rolls= new int [10000];
          for (int i = 0; i &lt; 10000; ++i)
              rolls[i] = Q.sample(); // autounboxing! Also cool!
          StdOut.printf("Mean (should be around 3.5): %5.4f\n", StdStats.mean(rolls));
          StdOut.printf("Standard deviation (should be around 1.7): %5.4f\n",
                  StdStats.stddev(rolls));
  
          // Now remove 3 random values
          StdOut.printf("Removing %d %d %d\n", Q.dequeue(), Q.dequeue(), Q.dequeue());
          // Add 7,8,9
          for (int i = 7; i &lt; 10; ++i) Q.enqueue(i);
          // Empty the queue in random order
          while (!Q.isEmpty()) StdOut.print(Q.dequeue() +" ");
          StdOut.println();
  
          // Let's look at the iterator. First, we make a queue of colours:
          RandomQueue&lt;String&gt; C= new RandomQueue&lt;String&gt;();
          C.enqueue("red"); C.enqueue("blue"); C.enqueue("green"); 
          C.enqueue("yellow");
  
          Iterator&lt;String&gt; I = C.iterator();
          Iterator&lt;String&gt; J = C.iterator();
  
          StdOut.print("Two colours from first shuffle: "+I.next()+" "+I.next()+" ");
  
          StdOut.print("\nEntire second shuffle: ");
          while (J.hasNext()) StdOut.print(J.next()+" ");
  
          StdOut.println("\nRemaining two colours from first shuffle: "+I.next()+" "+I.next()); // line 142
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/C1eCH.png" rel="nofollow noreferrer">I compile in cmd and this is the error I get</a></p>
  
  <p>the error happens here:
  <a href="https://i.stack.imgur.com/A0O56.png" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p>and here:
  <a href="https://i.stack.imgur.com/bxg54.png" rel="nofollow noreferrer">enter image description here</a></p>