Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-3332

NullPointerException when using a update document of type BasicDBObject and POJO codec

      When trying to update with a update document defined using a BasicDBObject and the update document has custom POJOs as part of its update definition the operation fails with the exception,

      Exception in thread "main" java.lang.NullPointerException
       at org.bson.codecs.pojo.PojoCodecImpl.encodeValue(PojoCodecImpl.java:180)
       at org.bson.codecs.pojo.PojoCodecImpl.encodeProperty(PojoCodecImpl.java:168)
       at org.bson.codecs.pojo.PojoCodecImpl.encode(PojoCodecImpl.java:105)
       at org.bson.codecs.pojo.AutomaticPojoCodec.encode(AutomaticPojoCodec.java:50)
       at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:231)
       at com.mongodb.DBObjectCodec.encodeIterable(DBObjectCodec.java:291)
       at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:218)
       at com.mongodb.DBObjectCodec.encodeMap(DBObjectCodec.java:240)
       at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:216)
       at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:148)
       at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:64)
       at org.bson.BsonDocumentWrapper.getUnwrapped(BsonDocumentWrapper.java:195)
       at org.bson.BsonDocumentWrapper.isEmpty(BsonDocumentWrapper.java:115)
       at com.mongodb.operation.BulkWriteBatch$WriteRequestEncoder.encode(BulkWriteBatch.java:395)
       at com.mongodb.operation.BulkWriteBatch$WriteRequestEncoder.encode(BulkWriteBatch.java:377)
       at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
       at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
       at com.mongodb.internal.connection.BsonWriterHelper.writeDocument(BsonWriterHelper.java:75)
       at com.mongodb.internal.connection.BsonWriterHelper.writePayload(BsonWriterHelper.java:59)
       at com.mongodb.internal.connection.CommandMessage.encodeMessageBodyWithMetadata(CommandMessage.java:145)
       at com.mongodb.internal.connection.RequestMessage.encode(RequestMessage.java:138)
       at com.mongodb.internal.connection.CommandMessage.encode(CommandMessage.java:59)
       at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:244)
       at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:99)
       at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:444)
       at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:72)
       at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:200)
       at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:269)
       at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:131)
       at com.mongodb.operation.MixedBulkWriteOperation.executeCommand(MixedBulkWriteOperation.java:433)
       at com.mongodb.operation.MixedBulkWriteOperation.executeBulkWriteBatch(MixedBulkWriteOperation.java:259)
       at com.mongodb.operation.MixedBulkWriteOperation.access$700(MixedBulkWriteOperation.java:70)
       at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:203)
       at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:194)
       at com.mongodb.operation.OperationHelper.withReleasableConnection(OperationHelper.java:424)
       at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:194)
       at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:69)
       at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:193)
       at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:961)
       at com.mongodb.client.internal.MongoCollectionImpl.executeUpdate(MongoCollectionImpl.java:952)
       at com.mongodb.client.internal.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:614)
       at com.mongodb.client.internal.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:609)
       at com.github.raghuchandrasekaran.app.ItemListDaoImpl.updateItems(ItemListDaoImpl.java:33)
       at com.github.raghuchandrasekaran.app.Application.main(Application.java:47)

      This is a small sample which can be used to reproduce the issue,

       

      BasicDBObject itemsUpdateDoc = new BasicDBObject();
       itemsUpdateDoc.put("items", <list of custom POJOs>);
      getCollection().updateOne(Filters.eq(<id>), new BasicDBObject("$addToSet", itemsUpdateDoc));
      

      The same problem will not occur when using the Updates builder or using a Document instead of BasicDBObject.

      The issue occurs because a null reference of EncoderContext is passed in the methods encodeMap and encodeIterable in DBObjectCodec. Also in the previous versions the PojoCodecImpl's encodeProperty did not make use of the passed encoderContext and was using the property model's cached codec to encode.

      propertyModel.getCachedCodec().encode
      

      This issue is also fixed in 3.11.0-beta4 as part of #JAVA-3157
       

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            raghu.9208@gmail.com Raghu c
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: