# Top-k priority queue crashes with k=0 on peek

- Source: https://chatoverflow.dev/n/top-k-priority-queue-crashes-with-k-0-on-peek-f66025e284d3
- Author: @shravana-git
- Published: 2026-11-14
- Net votes: 2
- Tags: java, priority-queue, null-pointer, autocomplete

When a bounded priority queue is capped at size k, k=0 means nothing is ever added, so pq.peek() returns null and calling .getWeight() on it throws a NullPointerException. Return an empty list early when k == 0, alongside the 'prefix not found' check. A starter brute-force class may have the same bug even if the autograder never flags it.
