Inserting objects with null values causes NullPointerException

11 views
Skip to first unread message

ra.ra...@gmail.com

unread,
Nov 25, 2017, 4:21:49 AM11/25/17
to ReactiveMongo - http://reactivemongo.org
Please see the test case written here. If confirmed I will will publish as an issue on GitHub repo.


I think that BSONDocument doesn't allow null in document. When lazy reading values later from its internal stream while insert will cause NullPointerException. NPE also happens when calling BSONDocument.pretty() on such a document.

The fix should be that such Writer should convert these Java's null to either scala's None or BSONull.

Cédric Chantepie

unread,
Nov 25, 2017, 8:20:54 AM11/25/17
to ReactiveMongo - http://reactivemongo.org
In anyway, using null is a bad practice.

ra.ra...@gmail.com

unread,
Nov 28, 2017, 2:42:10 AM11/28/17
to ReactiveMongo - http://reactivemongo.org
Agreed! While scala lives in JVM system, null's will be there and many users have identified they get random NullPointerException when using reactivemongo. The exception thrown quite later doesn't help at all. I have following solutions in mind:

1. Give users message that NullPointerException is caused by which field being null. This is at least more helpful than current message.
2. BSON writers should handle null to BSONull or None.

Lemme know if you are accepting patches for same.

Cédric Chantepie

unread,
Nov 28, 2017, 4:11:53 AM11/28/17
to ReactiveMongo - http://reactivemongo.org
Fixing Option.get call inside handler (getAs.get) is not the responsibility of the driver for me.

Such practice, whatever is the Option producer as not specific to the driver, swallow the error cause.

If the field is not optional rather use/compose using getAsTry .

Reply all
Reply to author
Forward
0 new messages