Mongodb java.util.NoSuchElementException:null 已解决

 1 public static String find(){
 2     String a="";
 3         try{
 4             MongoCollection<Document> collection = getCollection("School","student");  //数据库名:School 集合名:student
 5             //通过游标遍历检索出的文档集合 
 6 //          MongoCursor<Document>  cursor= collection.find(new Document("sname","Mary")). projection(new Document("sname",1).append("sage",1).append("_id", 0)).iterator();   //find查询条件:sname='Mary'。projection筛选:显示sname和sage,不显示_id(_id默认会显示)
 7             //查询所有数据
 8             MongoCursor<Document>  cursor= collection.find().iterator();
 9             while(cursor.hasNext()){
10                 a=cursor.next().toJson();
11                 System.out.println(cursor.next().toJson());
12             }
13         }catch(Exception e){
14             System.err.println( e.getClass().getName() + ": " + e.getMessage() );
15         }
16 return a;
17     }

主要是因为cursor.next().toJson();

运行了两边,改一下就好了

原文地址:https://www.cnblogs.com/smartisn/p/11722693.html