Skip to content

Commit 8951a33

Browse files
akarnokdakarnokd
authored andcommitted
CompositeException extra NPE protection
1 parent c833083 commit 8951a33

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/main/java/rx/exceptions/CompositeException.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,19 @@ public final class CompositeException extends RuntimeException {
4949
public CompositeException(String messagePrefix, Collection<? extends Throwable> errors) {
5050
Set<Throwable> deDupedExceptions = new LinkedHashSet<Throwable>();
5151
List<Throwable> _exceptions = new ArrayList<Throwable>();
52-
for (Throwable ex : errors) {
53-
if (ex instanceof CompositeException) {
54-
deDupedExceptions.addAll(((CompositeException) ex).getExceptions());
55-
} else {
56-
deDupedExceptions.add(ex);
52+
if (errors != null) {
53+
for (Throwable ex : errors) {
54+
if (ex instanceof CompositeException) {
55+
deDupedExceptions.addAll(((CompositeException) ex).getExceptions());
56+
} else
57+
if (ex != null) {
58+
deDupedExceptions.add(ex);
59+
} else {
60+
deDupedExceptions.add(new NullPointerException());
61+
}
5762
}
63+
} else {
64+
deDupedExceptions.add(new NullPointerException());
5865
}
5966

6067
_exceptions.addAll(deDupedExceptions);

src/test/java/rx/exceptions/CompositeExceptionTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,17 @@ private static Throwable getRootCause(Throwable ex) {
165165
}
166166
}
167167
}
168+
169+
@Test
170+
public void testNullCollection() {
171+
CompositeException composite = new CompositeException(null);
172+
composite.getCause();
173+
composite.printStackTrace();
174+
}
175+
@Test
176+
public void testNullElement() {
177+
CompositeException composite = new CompositeException(Arrays.asList((Throwable)null));
178+
composite.getCause();
179+
composite.printStackTrace();
180+
}
168181
}

0 commit comments

Comments
 (0)