Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Fixed
-
4.2
-
Collection, Map, Set
-
Windows 10
Eclipse IDE for Java Developers Version: 2018-09 (4.9.0)
Description
If we first add a null element into CompositeCollection by CompositeCollection.addComposited(nullElement), projects will crash and throw java.lang.NullPointerException when we call CompositeCollection.size().
For example, we have following code:
ArrayList<String> nullList = null;
CompositeCollection<String> cc = new CompositeCollection<String>(list);
cc.addComposited(nullList);
System.out.println(cc.size());
We will get:
Exception in thread "main" java.lang.NullPointerException
at org.apache.commons.collections4.collection.CompositeCollection.size(CompositeCollection.java:103)
Because in CompositeCollection.size(), it iterate all items in the Collection, call item.size() and sum up their size. If the item point to null, the program will crash.
This bug also exists in other methods of CompositeCollection. Moreover, CompositeSet and CompositeMap have the same bug.