mongodb kursor istisnosi - Java

Foydalanuvchilarning ma'lumotlarini saqlash uchun mongodb foydalanayapman. JB-dan ma'lumot oladigan, Player moslamalarini yaratadigan va ularni Oynalar qatoriga qo'shadigan usul yaratmoqchi edim.

bu quyidagi usul

public ArrayList getArrayOfPlayers(){
    ArrayList savePlayers = new ArrayList();
    DB db = connectToMongo();
    DBCollection coll = db.getCollection("players");
    DBCursor cursor = coll.find();

        while(cursor.hasNext()) {
            String tempName = (String)cursor.next().get("name");
            String tempSession = (String)cursor.next().get("session");
            String tempStringScore = (String)cursor.next().get("score");

            int tempScore = Integer.parseInt(tempStringScore);

            Player player = new Player(tempName,tempSession,tempScore);
            savePlayers.add(player);
        }


    return savePlayers;
}

JBda saqlangan 4 nafar foydalanuvchi bor va men birinchi usulni qidirishga harakat qilaman va keyinchalik nomlarni kiritish uchun istisno qilayapman. Men usuldan tashqari harakat qilardim va istisno tutdim, ammo keyin faqat birinchi foydalanuvchining nomini yozib qo'ydim. Ikkinchi iteratsiya istisnosini tashlaydi.

Men qabul qilgan istisnosiz xabar.

java.lang.RuntimeException: no more
com.mongodb.DBApiLayer$Result.next(DBApiLayer.java:394)
com.mongodb.DBApiLayer$Result.next(DBApiLayer.java:360)
com.mongodb.DBCursor._next(DBCursor.java:445)
com.mongodb.DBCursor.next(DBCursor.java:525)
machine.DAOMongodb.getArrayOfPlayers(DAOMongodb.java:74)
machine.testDB.doGet(testDB.java:43)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
7

1 javoblar

cursor.next() usulini chaqirib, siz keyingi elementni va kursorni oshirasiz. Har bir iteratsiya uchun cursor.next() 3 marta qo'ng'iroq qilasiz, shuning uchun ikkinchi ikkilamchi kursorda "yo'q" elementga egasiz. O'zgaruvchining mahalliy qismidagi iteratsiyada elementni saqlang:

while(cursor.hasNext()) {
        DBObject tobj = cursor.next();  
        String tempName = (String)tobj.get("name");
        String tempSession = (String)tobj.get("session");
        String tempStringScore = (String)tobj.get("score");

        int tempScore = Integer.parseInt(tempStringScore);

        Player player = new Player(tempName,tempSession,tempScore);
        savePlayers.add(player);
    }
9
qo'shib qo'ydi
Jozibasi kabi ishladilar !!! Rahmat!
qo'shib qo'ydi muallif x_maras, manba