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

NullPointerException when authentication used with replica sets not logged correctly

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.5
    • Affects Version/s: 2.4, 2.5
    • Component/s: Cluster Management
    • None
    • Environment:
      OS X 10.6.6, Java driver. 3-node replica set with user authentication (--auth) (and replica authentication --keyFile)

      If you try use the java driver to connect to a database that requires authentication, a NullPointerException is thrown and caught in Node.update(). This is because at the point the update is attempted, the java code has yet to call the DB.authenticate() method. Because the exception is already caught, it is not preventing anything from working. However the code should handle this better so as to not generate a NPE in the first place.

      String setName = config.get("_id").toString(); // <-- .toString() triggers NPE if DB.authenticate() hasn't yet been called.
      if ( _setName == null )

      { _setName = setName; _logger = Logger.getLogger( _rootLogger.getName() + "." + setName ); }

      Inspecting config shows it has $err defined (unauthenticated message) as well as code (10057). Suggest checking if null first, then checking if an error exists and if it's not authentication related, only then show the user. Another alternative would be to have DBPort check for the condition first and then throw a MongoInternalException.

      static final int UNAUTHENTICATED_ERROR_CODE = 10057;
      ...

      Object setNameObj = config.get("_id");
      if (setNameObj == null) {
      if (config.get("$err") != null && UNAUTHENTICATED_ERROR_CODE != (Integer)config.get("code"))

      { _logger.log( Level.SEVERE, "can't get set name from node: " +_addr + ". Error: " + config.get("$err")); }

      return;
      }
      String setName = setNameObj.toString();

      Stack Trace (based on git head, commit a052b4f35af4069121cbf47adc88d6199563c4d4):

      Feb 10, 2011 3:53:17 PM com.mongodb.ReplicaSetStatus$Node update
      SEVERE: unexpected error getting config from node: 10.211.55.23:23003
      java.lang.NullPointerException
      at com.mongodb.ReplicaSetStatus$Node.update(ReplicaSetStatus.java:174)
      at com.mongodb.ReplicaSetStatus.updateAll(ReplicaSetStatus.java:287)
      at com.mongodb.ReplicaSetStatus$Updater.run(ReplicaSetStatus.java:248)
      Feb 10, 2011 3:53:17 PM com.mongodb.ReplicaSetStatus$Node update
      SEVERE: unexpected error getting config from node: 10.211.55.23:23003
      java.lang.NullPointerException
      at com.mongodb.ReplicaSetStatus$Node.update(ReplicaSetStatus.java:174)
      at com.mongodb.ReplicaSetStatus.updateAll(ReplicaSetStatus.java:287)
      at com.mongodb.ReplicaSetStatus.ensureMaster(ReplicaSetStatus.java:280)
      at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:364)
      at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:206)
      at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:271)
      at com.mongodb.DB.command(DB.java:154)
      at com.mongodb.DB.command(DB.java:139)
      at com.mongodb.DB._doauth(DB.java:469)
      at com.mongodb.DB.authenticate(DB.java:434)

            Assignee:
            antoine Antoine Girbal
            Reporter:
            mikec Mike Copley
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: