title: Java ArrayQueue enqueue and dequeue throwing arrayQueueException
body: <p>I was having some problem when trying to dequeue from an ArrayQueue> variable type to ArrayQueue and pass it as a parameter to another method.</p>
  
  <p>So basically I got two methods, one is sort() with no parameter and the other one is merge() which takes in 2 ArrayQueue parameters. </p>
  
  <p>I got a global variable called Queue:</p>
  
  <pre><code>private ArrayQueue&lt;ArrayQueue&lt;E&gt;&gt; Queue;
  </code></pre>
  
  <p>And inside my sort(), I am dequeueing the first two items from Queue and pass them to merge() as parameters:</p>
  
  <pre><code>public void sort() {
      while (Queue.size() &gt;= 2) {
          ArrayQueue&lt;E&gt; first = Q.dequeue();
          ArrayQueue&lt;E&gt; second = Q.dequeue();
          System.out.print("1st" + firstElement);
          System.out.print("2nd" + secondElement);
           merge(first,second)
      }
  }
  
  public E dequeue() throws ArrayQueueException {
      if (isEmpty() == true)
          throw new ArrayQueueException("Queue error");
      if (front == rear) {
          front = front - 1;
          rear = rear - 1;
      } else
          front = front + 1;
      size--;
      return Q[front];
  }
  </code></pre>
  
  <p>Then inside my merge:</p>
  
  <pre><code>private ArrayQueue&lt;E&gt; merge(ArrayQueue&lt;E&gt; q1, ArrayQueue&lt;E&gt; q2) throws ArrayQueueException {
      System.out.println("q1 is " + q1.toString());
      ArrayQueue&lt;E&gt; merged = new ArrayQueue&lt;E&gt;();
      String e1 = (String) q1.dequeue();
      System.out.println("e1 is " + e1);
      String e2 = (String) q2.dequeue();
  
      merged.enqueue(q2.dequeue());
              if (q2.isEmpty()) {
                  // add remaining q1 elements
                  while (!q1.isEmpty()) {
                      merged.enqueue(q1.dequeue());
                  }
                  break;
              }
              // take another element from q2
              e2 = (String) q2.dequeue();
  }
  </code></pre>
  
  <p>And the output that I get from console is:</p>
  
  <pre><code>1st[the]2nd[ragged]q1 is [the]
  e1 is null
  Exception in thread "main" ArrayQueueException: Queue error
  at ArrayQueue.dequeue(ArrayQueue.java:84)
  at QueueSort.merge(QueueSort.java:41)
  at QueueSort.sort(QueueSort.java:73)
  at QueueSort.main(QueueSort.java:122)
  </code></pre>
  
  <p>QueueSort.java:41 is this line: merged.enqueue(q2.dequeue()); I not sure why it won't dequeue. </p>
  
  <p>Also, I not sure why the e1 became null because I was trying to perform a dequeue and cast it to string. I need this string for the later part.
  Any ideas? Thanks in advance.</p>