I’m making a library for MongoDB in Java. In one place I do:
myCollection.insertOne(doc).getInsertedId().getValue()
I insert a document and retrieve the id to return it to the caller. This works fine when testing the library, but when I actually incorporate it in a bigger project, the call returns:
Exception: java.lang.NullPointerException: Cannot invoke "com.mongodb.client.result.InsertOneResult.getInsertedId()" because "result" is null
I’m using mongodb-driver-sync
version 5.0.1
. Looking around, I see there’s another library using mongo-java-driver
version 3.12.10
where indeed there is a version of insertOne()
that returns void
.
How can I resolve this conflict?