编译器给出NullPointerException错误,不知道为什么。请帮忙吗?
public List<DBObject> findByDateDescending(int limit) {
List<DBObject> posts = null;
// XXX HW 3.2, Work Here
// Return a list of DBObjects, each one a post from the posts collection
DBCursor cur=postsCollection.find().sort(new BasicDBObject("date",-1)).limit(limit);
while(cur.hasNext()){
/*==>> error */ posts.add(cur.next());
}//end while
return posts;
}
答案 0 :(得分:4)
改变这个:
List<DBObject> posts = null;
对此:
List<DBObject> posts = new ArrayList<DBObject>();