rakemen
rakemen

Reputation: 348

cursor.next() always returns "java.util.NoSuchElementException"

I am writing a program in java which reads query results from MongoDB.

Below is the code (which gives an exception):

if(output != null){
                BasicDBObject whereQuery = new BasicDBObject();
                DBCursor cursor = null;
                for (DBObject obj : output.results()) {
                    String UserID = obj.get("UserID").toString();
                    whereQuery.put("User_ID", UserID);                                                              

                    cursor = users.find(whereQuery);
                    System.out.println(cursor.toString());
                    System.out.println("This user name who tweeted "+ cursor.next());


                }
            }

When I run this code, only the first value of cursor gets printed using the sysout statement in the last line, the second value which comes from the 'for' loop results in an exception. Wonder why?

The sysout statement just before the cursor.next() prints the current value in the cursor all the time, placed it there to verify whether the cursor actually has a value.

I have tried the hasNext() while loop for the cursor.next(), it only enters the loop for the first time the rest of the checks it just jumps out.

Error stack trace:
   Exception in thread "main" java.util.NoSuchElementException
   at com.mongodb.QueryResultIterator.next(QueryResultIterator.java:111)
   at com.mongodb.DBCursor._next(DBCursor.java:510)
   at com.mongodb.DBCursor.next(DBCursor.java:581)
   at ReadData.referencedDataModel(ReadData.java:97)
   at ReadData.main(ReadData.java:131)

Upvotes: 1

Views: 3120

Answers (2)

rakemen
rakemen

Reputation: 348

if(output != null){
                BasicDBObject whereQuery = new BasicDBObject();
                DBCursor cursor;

                for (DBObject obj : output.results()) {

                    String UserID = obj.get("UserID").toString();
                    System.out.println("The real results "+ UserID);

                    whereQuery.put("User_ID", UserID);                                                  
                    DBObject value = users.findOne(whereQuery);
                    if(value != null)
                        System.out.println("The user who used the word in the tweet: " +value.get("User_Name").toString());
                    else
                        System.out.println("The user profile is not available");
                }

            }

Screw cursors!

Thanks @Disposer!

Upvotes: 1

Disposer
Disposer

Reputation: 6371

I'm not sure its your answer, but it's worth a try:

String UserID = obj.get("UserID").toString();
cursor = users.find(whereQuery);
if(cursor.hasNext()) {
    // the object will be your object, its basically a json document
    BasicDBObject obj = cursor.next(); 

    // print obj to console as json file
    System.out.println(obj);

    // you can get any property with obj.get...(); method such as .getString()
    // The below code will extract the User_ID from the object you received via find query 
    string id = obj.getString("User_ID");
}

If you are sure the result will have just one document you can use this code:

BasicDBObject obj = users.findOne(whereQuery);

Very useful resource about java and Mongo:

http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/

Upvotes: 3

Related Questions