Announcement Announcement Module
Collapse
No announcement yet.
Mongodb: NullPointerException when trying to store a file to GridFS with spring-data Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mongodb: NullPointerException when trying to store a file to GridFS with spring-data

    Hello,

    I'm trying to get GridFS to work with spring-data and MongoDB. I can use queries over mongoTemplate. This is working. But getting GridFS to work seems to be a bit more bitchy. I try to store a file over a Unit Test but get a NullPointerException when the test is executed as shown below.

    I'm thankful for any hints in to the right direction. Especially also links to documentation or code samples that may help.

    I'm using following versions:
    * Spring 3.1.2.RELEASE
    * Mongo Java Driver 2.9.1
    * Spring Data MongoDB 1.1.1.RELEASE

    I get following error when I execute my test:
    Code:
    java.lang.NullPointerException
    	at com.eerra.cardkeeperweb.mongodb.GridFsTest.storeFileTest(GridFsTest.java:47)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:601)
    	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    	at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    	at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    	at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:601)
    	at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    	at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    	at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
    	at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
    	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
    spring-data.xml configuration:
    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:c="http://www.springframework.org/schema/c"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mongo="http://www.springframework.org/schema/data/mongo"
           xmlns:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="
    [url]http://www.springframework.org/schema/beans[/url]
    [url]http://www.springframework.org/schema/beans/spring-beans-3.1.xsd[/url]
    [url]http://www.springframework.org/schema/tx[/url]
    [url]http://www.springframework.org/schema/tx/spring-tx-3.1.xsd[/url]
    [url]http://www.springframework.org/schema/context[/url]
    [url]http://www.springframework.org/schema/context/spring-context-3.1.xsd[/url]
    [url]http://www.springframework.org/schema/data/mongo[/url]
    [url]http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd[/url]
    [url]http://www.springframework.org/schema/util[/url]
    http://www.springframework.org/schema/util/spring-util-3.1.xsd">
    
        <context:property-placeholder properties-ref="deployProperties" />
    
        <!-- Activate Spring Data MongoDB repository support -->
        <mongo:repositories base-package="com.eerra.cardkeeperweb.repository" />
    
        <!-- MongoDB host -->
        <mongo:mongo host="${mongo.host.name}" port="${mongo.host.port}"/>
    
        <!-- Template for performing MongoDB operations (necessary for mapping) -->       
    	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    		<constructor-arg ref="mongo"/>
    		<constructor-arg name="databaseName" value="${mongo.db.name}"/>
    	</bean>       
        
    	<!-- Create MongoDB Factory for GridFS -->
    	<mongo:db-factory dbname="${mongo.db.name}" host="${mongo.host.name}" port="${mongo.host.port}" />
     	
     	<mongo:mapping-converter id="converter" db-factory-ref="mongoDbFactory" />
    
    	<!-- MongoDB GridFS Template -->
    	<bean id="gridTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
    		<constructor-arg ref="mongoDbFactory" />
    		<constructor-arg ref="converter" />
    	</bean>  
    
        <!-- Service for initializing MongoDB with sample data using MongoTemplate -->
        <bean id="initMongoService" class="com.eerra.cardkeeperweb.service.InitMongoService" init-method="init"/>	
    </beans>
    My Unit test looks as followed:
    -> res is set with the image, i debugged that.
    Code:
    public class GridFsTest {
       
        @Autowired
        GridFsTemplate template;
        
        @Test
        public void storeFileTest() throws IOException {
            Resource res = new ClassPathResource("mvc.jpg");
    
            template.store(res.getInputStream(), "mvc.jpg");
            
            assertEquals("Store File Test: ", "idontcare", "idontcare");
        }
    }
    Thanks for your input.

    Kind regards,
    Chris
Working...
X