java.lang.IllegalArgumentException: can't serialize class java.lang.Character

830 views
Skip to first unread message

HP

unread,
May 30, 2011, 11:49:59 PM5/30/11
to mongodb-user
Hello,

I did a basic test on monogoDB version 1.8.1 with Java. The system
alway return me an exception.
I attached my code and all return message as below. I have checked my
code many times, and can not find where is the problem. Please help
to find out the problem for me.

Thanks,

import java.net.UnknownHostException;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class QMater_IO {

public static void printAll_QMaster(DBCollection collection){
DBCursor cursor = collection.find();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
}

public static void removeAll_QMaster(DBCollection coll){
coll.remove(new BasicDBObject());
}

public static void insert_QMaster(DBCollection collection){
// int number =0;
System.out.println("Insert routing start..");

BasicDBObject document = new BasicDBObject();
document.put("Number", 100);
document.put("Type", 'A');
document.put("Code", "1100");
document.put("Unit", "ABCD");

collection.insert(document);
System.out.println("Insert routing end..");
}

public static void main(String[] args) {

try {

Mongo mongo = new Mongo();
DB db = mongo.getDB("test");

// get a single collection
DBCollection collection = db.getCollection("QMaster");
removeAll_QMaster(collection);
System.out.println("Testing 1.Print all..");

printAll_QMaster(collection);


insert_QMaster(collection);
System.out.println("insert 1..");
printAll_QMaster(collection);


System.out.println("Done");

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}

}


}

Exception in thread "main" java.lang.IllegalArgumentException: can't
serialize class java.lang.Character
at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:213)
at org.bson.BSONEncoder.putObject(BSONEncoder.java:123)
at org.bson.BSONEncoder.putObject(BSONEncoder.java:69)
at com.mongodb.OutMessage.putObject(OutMessage.java:189)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:245)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:209)
at com.mongodb.DBCollection.insert(DBCollection.java:82)
at QMater_IO.insert_QMaster(QMater_IO.java:32)
at QMater_IO.main(QMater_IO.java:51)



Nat

unread,
May 30, 2011, 11:54:54 PM5/30/11
to mongod...@googlegroups.com
you just need to change

document.put("Type", 'A'); 

to

document.put("Type", "A"); 

as BSON doesn't support character type.

Scott Hernandez

unread,
May 30, 2011, 11:59:12 PM5/30/11
to mongod...@googlegroups.com
This 'A' is a char.

document.put("Type", 'A');


Use double quotes.




--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Reply all
Reply to author
Forward
0 new messages