javamongodbdbobject

array of DBObject with same keys each element


I am trying to create an array of DBObject, all of the elements have the same key with different values. what is the problem with this implementation ?

 DBObject[] Out = new BasicDBObject[2];

 out[0].put("VALUE","1");
 out[0].put("PROPERTY","1");

 out[1].put("VALUE","2");
 out[1].put("PROPERTY","2");

Solution

  • First, Out and out are mixed up (use upper/lower case consistently)

    Second, you need to initialize the objects in the array before you can use them:

    DBObject[] out = new BasicDBObject[2];
    
    out[0] = new BasicDBObject();
    out[0].put("VALUE","1");
    out[0].put("PROPERTY","1");
    
    out[1] = new BasicDBObject();
    out[1].put("VALUE","2");
    out[1].put("PROPERTY","2");