I have checked the implementation, it does so intentionally
public void put(E e) throws InterruptedException {
if (e == null) throw new NullPointerException();
This surprise is not convenient for user (who wants to signal end of stream this way, for instance) and breaks the general contract with collections, which easily accept the null elements. What is the point of BlockingQueue to discriminate null elements? If nulls are so bad, might be we should refrain using them at all and enforce this low in JLS?
Accepting nulls is not part of the
Collectioncontract. Indeed, theCollectionJavadoc specifically states:In many cases, adding
nullto a collection means there’s a bug in your program somewhere, not that you put it in deliberately. For example, the Guava library (which I contribute to) makes the explicit decision to reject nulls from many of their collection implementations, specifically the immutable ones:There are generally workarounds that do accept nulls, but many collection implementations make the decision to reject nulls (which most users find helpful, as it helps them find bugs) and offer workarounds for the rare case where explicit nulls are appropriate.
In all honesty, I think the reason that
LinkedBlockingQueueis in this category is that all this hadn’t been figured out when the original collections framework was developed, but it was pretty clear by the time that the concurrent collections were being added. Doug Lea, who did much of the work onutil.concurrent, has been quoted as saying,In the worst case, object wrappers or “poison objects” are always valid workarounds; Guava provides an
Optionalclass which can serve that role in many cases, which is discussed extensively here on StackOverflow.