title: Null pointer exception while making MongoDB connection on WAS 8.5 web application
body: <p>I am trying to connect MongoDB via web application hosted on IBM WAS 8.5. While making connection to MongoDB which is a sharded cluster i am getting below error -</p>
  
  <pre><code>com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=l4dreqdb4967.hewitt.com:30000, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoException: java.lang.NullPointerException}, caused by {java.lang.NullPointerException}}, {address=l4dreqdb4968.hewitt.com:30000, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoException: java.lang.NullPointerException}, caused by {java.lang.NullPointerException}}]
           at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:377)
           at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:104)
           at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.(ClusterBinding.java:75)
           at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.(ClusterBinding.java:71)
           at com.mongodb.binding.ClusterBinding.getReadConnectionSource(ClusterBinding.java:63)
           at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:393)
  </code></pre>
  
  <p>Mongo client being used is - 3.5 <br>
  Mongo DB  vesrion - 4 <br>
  IT is an SSL connection and we have put the cert file into JDK. <br>
  code used to make connection is - </p>
  
  <pre><code>try {
      MongoClientURI connectionString;
      MongoClientOptions.Builder optionsBuilder;
      optionsBuilder = MongoClientOptions.builder()
                          .sslEnabled(true)
                          .sslInvalidHostNameAllowed(true);
      connectionString = new MongoClientURI(dbURI, optionsBuilder);
      mongoClient = new MongoClient(connectionString);
  
      MongoDatabase database = mongoClient.getDatabase("SPMQC");
      MongoCollection&lt;Document&gt; collection = database.getCollection("PersonDocumentData");
      Document mongoDoc = collection.find().first();  
  } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
  }
  </code></pre>