{"record":{"id":"3e1ec09539325dc4","repo":"stanfordnlp/CoreNLP","slug":"empty-pq","errorCode":null,"errorMessage":"Empty PQ","messagePattern":"Empty PQ","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/BinaryHeapPriorityQueue.java","lineNumber":44,"sourceCode":"    public E key;\n    public int index;\n    public double priority;\n\n    @Override\n    public String toString() {\n      return key + \" at \" + index + \" (\" + priority + ')';\n    }\n  }\n\n  @Override\n  public boolean hasNext() {\n    return size() > 0;\n  }\n\n  @Override\n  public E next() {\n    if (size() == 0) {\n      throw new NoSuchElementException(\"Empty PQ\");\n    }\n    return removeFirst();\n  }\n\n  @Override\n  public void remove() {\n    throw new UnsupportedOperationException();\n  }\n\n  /**\n   * {@code indexToEntry} maps linear array locations (not\n   * priorities) to heap entries.\n   */\n  private final List<Entry<E>> indexToEntry;\n\n  /**\n   * {@code keyToEntry} maps heap objects to their heap\n   * entries.","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/BinaryHeapPriorityQueue.java#L26-L62","documentation":"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.","triggerScenarios":"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().","commonSituations":"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.","solutions":["Check hasNext() (size() > 0) before each next() call.","Wrap drain loops in while (pq.hasNext()) { ... } rather than fixed-count loops.","Catch NoSuchElementException as a defensive guard if the empty case is expected and benign."],"exampleFix":"// before\nwhile (true) { E e = pq.next(); process(e); }\n// after\nwhile (pq.hasNext()) { E e = pq.next(); process(e); }","handlingStrategy":"try-catch","validationCode":"if (pq.size() == 0) { /* skip or break */ }","typeGuard":null,"tryCatchPattern":"try {\n  E e = pq.next();\n} catch (NoSuchElementException e) {\n  // queue exhausted: break out of the drain loop\n}","preventionTips":["Always drain with while (pq.hasNext()) not fixed-size loops.","Re-check emptiness after any operation that may remove elements.","In concurrent code, use isEmpty() inside a lock before consuming."],"tags":["java","priority-queue","no-such-element","empty-collection"],"backgroundTag":"empty-result-set","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}