Skip to content

Commit

Permalink
fix NullPointerException hazard in StringUtils.join(..) method
Browse files Browse the repository at this point in the history
Signed-off-by: WillardHu <[email protected]>
  • Loading branch information
WillardHu committed Sep 14, 2021
1 parent 0b5d227 commit 58771ba
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,11 @@ public String format(T obj) {

public static <T> String join(Collection<T> collection, String separator,
StringFormatter<T> formatter) {
Iterator<T> iterator = collection.iterator();
// handle null, zero and one elements before building a buffer
if (iterator == null) {
if (collection == null) {
return null;
}
Iterator<T> iterator = collection.iterator();
if (!iterator.hasNext()) {
return EMPTY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public void testIsNumeric() {
public void testJoin() {
Assert.assertEquals("", StringUtils.join(new ArrayList(), "1a 2b 3c"));

Assert.assertNull(StringUtils.join(null, "1a 2b 3c"));

ArrayList collection = new ArrayList();
collection.add(null);
Assert.assertEquals("", StringUtils.join(collection, "1a 2b 3c"));
Expand Down

0 comments on commit 58771ba

Please sign in to comment.