title: java.lang.IllegalStateException: open
body: <p>while I'm trying to read data from Mongodb I'm getting this Exception,</p>
  
  <pre><code>java.lang.IllegalStateException: open
      at org.bson.util.Assertions.isTrue(Assertions.java:36)
      at com.mongodb.DBTCPConnector.isMongosConnection(DBTCPConnector.java:369)
      at com.mongodb.Mongo.isMongosConnection(Mongo.java:645)
      at com.mongodb.DBCursor._check(DBCursor.java:454)
      at com.mongodb.DBCursor._hasNext(DBCursor.java:546)
      at com.mongodb.DBCursor.hasNext(DBCursor.java:571)
      at com.calsoftlabs.mongo.client.impl.ReadingData.main(ReadingData.java:93)
  </code></pre>
  
  <p>MyCode</p>
  
  <pre><code>    public static DBCursor firstRecord(String ip) throws Exception {
  
          DBObject query = new BasicDBObject("client_ip", ip);
  
          DBCollection collection = getConnection();
  
          DBObject obj = new BasicDBObject("_id", 1);
  
          DBCursor cursor = collection.find(query).sort(obj).limit(1);
  
          mongo.close();
  
          return cursor;
  
      }
      DBCursor cursor = ReadingData.firstRecord(ip);
  
                      while (cursor.hasNext()) { 
  
                          cursor.next();
  
                          myList.add(new BasicDBObject("client_ip", (String) cursor.curr().get(
                                  "client_ip")).append("timestamp",
                                  (String) cursor.curr().get("timestamp")).append("total_traffic",
                                  (String) cursor.curr().get("total_traffic")));
  
                      }
  </code></pre>
  
  <p>By using the above code I'm reading first record from set of records in Mongodb, but the line <code>cursor.hasNext()</code> throwing IllegalStateException. Please suggest me how to resolve this issue..</p>