Skip to content

Commit 3813249

Browse files
robruarozza
authored andcommitted
Check for for null collection elements in POJO CollectionCodec before deferring to codec.encode.
This fixes an NPE when a POJO has a nested collection element and one of the nested collection values is null. JAVA-2756
1 parent a034ec8 commit 3813249

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

bson/src/main/org/bson/codecs/pojo/CollectionPropertyCodecProvider.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ private static class CollectionCodec<T> implements Codec<Collection<T>> {
5353
public void encode(final BsonWriter writer, final Collection<T> collection, final EncoderContext encoderContext) {
5454
writer.writeStartArray();
5555
for (final T value : collection) {
56-
codec.encode(writer, value, encoderContext);
56+
if (value == null) {
57+
writer.writeNull();
58+
} else {
59+
codec.encode(writer, value, encoderContext);
60+
}
5761
}
5862
writer.writeEndArray();
5963
}

0 commit comments

Comments
 (0)