Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6759527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:57:11+00:00 2026-05-26T13:57:11+00:00

I am using mongodb to store information of users. I wanted to create a

  • 0

I am using mongodb to store information of users.
I wanted to create a method that get the information from the db, creates Player objects and inserts them in an array of Players.

this is the following method

public ArrayList<Player> getArrayOfPlayers(){
    ArrayList<Player> savePlayers = new ArrayList<Player>();
    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;
}

I have 4 users stored in the db and when I ‘m trying to first call the method and then print the names for example I’m getting an exception.
I used a try-catch outside the method’s while and I caught the exception, but then it printed the name of the first user only. It seems that it throws the exception in the second iteration.

Here is the message with the exception that I am receiving.

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:57:12+00:00Added an answer on May 26, 2026 at 1:57 pm

    Calling the cursor.next() method you get the next element and increment cursor position.
    You call cursor.next() 3 times for each iteration so at second iteration you have “no more” element in the cursor.
    Save the element in a variable local to the iteration:

    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);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Do you think that using a MongoDB Json Database to store log files from
Using MongoDB C# driver ( http://github.com/samus/mongodb-csharp ), seems that I'm unable to get the
I'm considering using MongoDb for a backing store for a WPF app that I'm
I need to create a server-side app that saving information to a mongoDB ,
I am considering using MongoDB to store documents that include a list of key/value
From MongoDB's webpage I understand that they are not supporting transactions fully, if any.
I'm using MongoDB to store user profiles, and now I want to use GridFS
For my case i am using MongoDB to store a whole bunch of products
Is it possible to create a Git repository that saves its objects into some
I'm designing an application that processes RSS feeds using MongoDB. Currently my collections are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.