stanfordnlp/CoreNLP · error · NoSuchElementException

Empty PQ

Error message

Empty PQ

What it means

BinaryHeapPriorityQueue implements an Iterator-like next() that returns and removes the element with minimum key. When the queue is empty there is no element to return, so it throws NoSuchElementException('Empty PQ') instead of returning null.

Solutions

  1. Check hasNext() (size() > 0) before each next() call.
  2. Wrap drain loops in while (pq.hasNext()) { ... } rather than fixed-count loops.
  3. Catch NoSuchElementException as a defensive guard if the empty case is expected and benign.

Example fix

// before
while (true) { E e = pq.next(); process(e); }
// after
while (pq.hasNext()) { E e = pq.next(); process(e); }
Defensive patterns

Strategy: try-catch

Validate before calling

if (pq.size() == 0) { /* skip or break */ }

Try / catch

try {
  E e = pq.next();
} catch (NoSuchElementException e) {
  // queue exhausted: break out of the drain loop
}

Prevention

When it happens

Trigger: Calling next() (or a next()-based iteration over the queue) when size() == 0, e.g. draining the queue one element past the last, or calling next() without first checking hasNext().

Common situations: while(true) drain loops without an emptiness check; interleaved code consuming the queue while another thread also drains it; off-by-one loops that call next() exactly size()+1 times.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/3e1ec09539325dc4. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/util/BinaryHeapPriorityQueue.java:44

    public E key;
    public int index;
    public double priority;

    @Override
    public String toString() {
      return key + " at " + index + " (" + priority + ')';
    }
  }

  @Override
  public boolean hasNext() {
    return size() > 0;
  }

  @Override
  public E next() {
    if (size() == 0) {
      throw new NoSuchElementException("Empty PQ");
    }
    return removeFirst();
  }

  @Override
  public void remove() {
    throw new UnsupportedOperationException();
  }

  /**
   * {@code indexToEntry} maps linear array locations (not
   * priorities) to heap entries.
   */
  private final List<Entry<E>> indexToEntry;

  /**
   * {@code keyToEntry} maps heap objects to their heap
   * entries.

View on GitHub (pinned to 1b7edd19c4)