MongoDB throws NullPointerException in Class DBTCPConnector

I'm trying to get a grip using MongoDB with the Spring Data MongoDB framework. I tried severeal approaches to connect to my local DB and insert + retrieve some collections and documents, using the official Spring Reference Documentation and some simple examples like this Hello-World-Demo.

Actually, for the beginning I'm going to use the MongoTemplate to keep it simple. But now I'run into the following problem.

When I use the Spring Configuration with annotations configure the setting needed to connect to my local DB, everything works fine. Otherwise When I use XML for the configuration, I run into an java.lang.NullPointerException at com.mongodb.DBTCPConnector.getClusterDescription

Here are my configuration files and the example code for connecting to the DB:

Use Case 1 - Spring Configuration with annotations:

//package, imports etc. here
public class MongoConfiguration {

    public @Bean MongoDbFactory mongoDbFactory() throws Exception {
    return new SimpleMongoDbFactory(new MongoClient(), "Test1");

    public @Bean MongoTemplate mongoTemplate() throws Exception {
    return new MongoTemplate(mongoDbFactory());

Using the configuration class like this ...

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MongoConfiguration.class);
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
for (String s : mongoOperation.getCollectionNames()) {

.. creates this output:


Use Case 2 - XML configuration (SpringConfig2.xml):

<beans xmlns=""

    <mongo:mongo host="" port="27017" />
    <mongo:db-factory dbname="Test1" />

    <bean id="mongoTemplate" class="">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />

Using the configuration file like this ...

GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig2.xml");
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
for (String s : mongoOperation.getCollectionNames()) {

.. results in this error

    at com.mongodb.DBTCPConnector.getClusterDescription(
    at com.mongodb.DBTCPConnector.getMaxBsonObjectSize(
    at com.mongodb.Mongo.getMaxBsonObjectSize(
    at com.mongodb.DBCollectionImpl.find(
    at com.mongodb.DBCollectionImpl.find(
    at com.mongodb.DB.getCollectionNames(
    at test.main(

When debugging DBTCPConnector.getClusterDescription, it seems that in the second case the private class variable cluster is for some reason not instantiated, leading to the described error.

What I'd like to know is: am I doing anything wrong within my XML-configuration or when using this config / context? Why does using XML-configuration end in an error, while using annotation configuration just works fine?

Basically (in the end) I just "copy+paste"'d the code examples from the official references for Spring / Spring Data MongoDB.

I'd appreciate any help / suggestions :)


Thanks to the hint from Tushar Mishra in the comments I was able to track down the origin of the error and why it occurs in one case but not in the other. I'll try to explain in short, maybe it'll save someone some research time (or remember me if I perhaps run into this or a similar error again).

When closing either the AnnotationConfigApplicationContext-object (UC1) or the AnnotationConfigApplicationContext-object (UC2) with ctx.close(), from somewhere org.springframework.beans.factory.DisposableBean#destroy() is invoked. As far as I understood, within there used singleton beans get destroyed by invoking the destroy()-methods of each of those beans.

In short: closing the XXApplicationContext-object also destroys the MongoFactoryBean and with it closes the connection to the MongoDB. So using c**tx.close()** at the described position leads to using a MongoOperations-object on a closed connection at the next line, resulting in the described NullPointerException.

And for why the sample code with Annotations (UC1) runs fine, whereas the sample code configured with XML (UC2) just breaks with an NullPointerException: In UC1, the org.springframework.beans.factory.DisposableBean#destroy()-call is interupted by some DisposableBeanMethodInterceptor, which tries to redirect to a dispose() method. But there is no method implemented in MongoFactoryBean, so the Mongo-connection stays alive and can be used even after ctx.close() was invoked. I think the connection will get killed later by the garbage collector, leading to the same error.

I'm not sure what to make of this, yet. If I should implement a dispose()-method myself or something alike. But at least I figured out, why this code sample behave different, although supposed to do the same thing.

Maybe it helps someone. Thanks again, Tushar, for giving a hint to the right direction.

Need Your Help

How to set cells' background?

c# excel openxml

How to set the background of several cells within a row (or of a whole row) in OpenXml?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.