-1

Will insert(List) return null?

WriteResult result = collection.insert(List<DBObject>);
result.getError()  -->Throws NullPointeException

In the above snippet, what may cause the return of null for WriteResult?

vivek_jonam
  • 3,237
  • 8
  • 32
  • 44

2 Answers2

0

Can you try providing a BasicDBList, which contains BasicDBObject instances.

For example:

BasicDBObject updateObject = new BasicDBObject();
BasicDBList dbList = createList(objects);
updateObject.append("$push", new BasicDBObject("collection", dbList));

WriteResult result = collection.insert(dbList);

...
private BasicDBList createList(List<SampleObject> list) {
  BasicDBList result = new BasicDBList();
  for (SampleObject obj: list) {
    BasicDBObject dbObject = new BasicDBObject();
    dbCar.append("name", obj.getName()); //for exmaple
    result.add(dbObj);
  }

 return result; 
}
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
0

It would probably return a NullPointerException because at the least the method is expecting a list that has at least one key to read in order to successfully insert something. Else it would be inserting, technically, a non-existent document.

Celestz
  • 301
  • 3
  • 7