swlnet / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

NullPointerException in Multiset.add #248

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
com.google.common.collect.Multiset
line 141

The @Nullable annotation on Multiset.add:

 int add(@Nullable E element, int occurrences);

indicates that add should always allow a nullable element to be added
to the Multiset, without causing a NullPointerException.

 private void multisetAdd(Multiset<Object> ms) {
     ms.add(null, 1); // never throws NullPointerException
 }

However, this unit test fails by throwing a NullPointerException:

 public void applyTest() {
     multisetAdd(ImmutableMultiset.of());
 }

The Javadoc for ImmutableMultiset, which is a subclass of Multiset,
indicates the following on line 35:

 * An immutable hash-based multiset. Does not permit null elements.

This is incompatible with the specification of Multiset, as expressed
by Multiset's annotations.

Proposed fix:  Remove the @Nullable annotation on line 141.  This
annotation is inconsistent with the Javadoc for add(E, int) and also
with add(E), which correctly lacks a "@Nullable" annotation on its
"element" argument.  Once the @Nullable annotation on line 141 is
removed, the Multiset annotations correctly reflect the fact that a
client cannot count on null definitely being a legal element.

A patch for the fix is in MultisetFix.patch

Original issue reported on code.google.com by mala...@gmail.com on 18 Sep 2009 at 11:03

Attachments:

GoogleCodeExporter commented 9 years ago
See the @Nullable discussion in issue 247.

Original comment by limpbizkit on 19 Sep 2009 at 6:32