0

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");
Ohad
  • 1,563
  • 2
  • 20
  • 44

2 Answers2

1

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");
Stefan
  • 2,395
  • 4
  • 15
  • 32
0

You are only creating array of references. You need to create objects for it before assigning values.

DBObject[] Out = new BasicDBObject[2];

// instantiating objects for the array
for(int i=0 ; i < Out.length ; i++){
    Out[i] = new BasicDBObject();
}

Out[0].put("VALUE","1");
Out[0].put("PROPERTY","1");
Out[1].put("VALUE","2");
Out[1].put("PROPERTY","2");
Tharun
  • 311
  • 2
  • 14