Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-605

NullPointerException on DBCursor.hasNext() when cursor is closed

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: API
    • None
    • Environment:
      Linux Ubuntu 11.04, mongod 2.0.5

      When iterating over a DBCursor using standard hasNext()/next() logic, hasNext() can raise a NullPointerException when the underlying server-side cursor has been closed. This happens in my code because I wrap the DBCursor in another Iterator that provides "batching": Iterates over the DBCursor generating a list of objects instead of a single object.

      I has been able to trace the issue to the following code fragments in DBApiLayer:

      public boolean hasNext(){
          boolean hasNext = _cur.hasNext();
          while ( !hasNext ) {
            ....
          }
          return hasNext;
      }
      ...
      void close(){
          if (_curResult != null) {
              killCursor();
              _curResult = null;
              _cur = null;
          }
      }
      

      Please the documentation tells the hasNext() method can throw a MongoException and there are some issues that deal with this API issue.

            Assignee:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Reporter:
            sthampy Sushil Thampy
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: