@Test
  public void writeNulls() throws IOException {
    BsonDocument doc = new BsonDocument();
    BsonWriter writer = new BsonWriter(new BsonDocumentWriter(doc));
    writer.beginObject();
    writer.name("nullString"); writer.value((String) null);
    writer.name("nullBoolean"); writer.value((Boolean) null);
    writer.name("nullNumber"); writer.value((Long) null);
    writer.name("null"); writer.nullValue();
    writer.endObject();
    writer.flush();
    check(doc.get("nullString")).is(BsonNull.VALUE);
    check(doc.get("nullBoolean")).is(BsonNull.VALUE);
    check(doc.get("nullNumber")).is(BsonNull.VALUE);
    check(doc.get("null")).is(BsonNull.VALUE);
  }
Check that writing nulls does not cause NPE
mongo/test/org/immutables/mongo/bson4gson/BsonWriterTest.java