I have a problem in my code. I have a collection of objects in Mongo, all of which have a "name", "description", "imageURL", "code".
I want to do a search of my collection and return ArrayList of all the names, descriptions,codes and imageURLs
I tried it like this
else{
DBCursor cursair = this.searchItem(code);
while(cursair.hasNext()){
this.searchNames = new ArrayList<String>();
this.searchBarcodes = new ArrayList<String>();
this.searchDescriptions = new ArrayList<String>();
this.searchImageURLs = new ArrayList<String>();
this.searchNames.add(cursair.curr().get(ITEMS_NAME).toString());
this.searchDescriptions.add(cursair.curr().get(ITEMS_DESCRIPTION).toString());
this.searchImageURLs.add(cursair.curr().get(ITEMS_IMAGEURL).toString());
this.searchBarcodes.add(cursair.next().get(ITEMS_CODE).toString());
}
I have a problem with it as it gives me a NullPointerException at this.searchNames.add(cursair.curr().get(ITEMS_NAME).toString());
. ITEMS_NAME
is a String constant defined in the code.
the searchItem(code)
method looks like this
public DBCursor searchItem(String searchQuery){
BasicDBObject query = new BasicDBObject();
query.put(ITEMS_NAME, java.util.regex.Pattern.compile(searchQuery));
DBCursor cursor = itemsCollection.find(query);
return cursor;
}
I also checked in my debugger, when I call cursor.toArray()
it returns a List<DBObject>
with a size of 5, so I am pretty confident about cursor having the right information in itself
您想调用cursair.next()
作为while循环的第一步,并使用返回的DBObject
来提取数据。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.