Results 1 to 7 of 7

Thread: NullPointerException from DAO by using crudRepositoy

  1. #1
    Join Date
    Jun 2012
    Posts
    5

    Default NullPointerException from DAO by using crudRepositoy

    Hi,

    i got java.lang.NullPointerException by using CrudRepository

    my project looks like this:

    strut2 action
    service
    repository
    domain

    Code:
    class Action {
      @Autowired MyService service;
      public String execute(){
        service.getList();
        return "ok";
      }
    }
    
    interface Service {
      List getList();
    }
    
    @org.springframework.stereotype.Service
    class ServiceImpl implements Service {
      @Autowired MyRepository repo;
      List getList(){
        return (List)repo.findAll();
      }
    }
    package com.mycompany.repositories;
    interface MyRepository extends CrudRepository<MyPojo, String>{}
    here is the applicationContext.xml:

    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:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:jpa="http://www.springframework.org/schema/data/jpa"
           xsi:schemaLocation=" http://www.springframework.org/schema/beans 
                                http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                                http://www.springframework.org/schema/context
                                http://www.springframework.org/schema/context/spring-context-3.1.xsd
                                http://www.springframework.org/schema/tx
                                http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
                                http://www.springframework.org/schema/data/jpa 
                                http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
        
        <jpa:repositories base-package="com.mycompany.repositories" />
        
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
            <property name="entityManagerFactory" ref="entityManagerFactory" />
            <property name="jpaDialect">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
            </property>
        </bean>
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="persistenceUnitName" value="unit-name" />
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="showSql" value="true" />
                    <property name="generateDdl" value="true" />
                    <property name="database" value="POSTGRESQL"/>
                </bean>
            </property>
        </bean>
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="username" value="root" />
            <property name="password" value="root" />
            <property name="driverClassName" value="org.postgresql.Driver" />
            <property name="url" value="jdbc:postgresql://127.0.0.1:5432/mydb" />
        </bean>
        
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"></bean>  
    
        <tx:annotation-driven transaction-manager="transactionManager" />
    </beans>
    I'm new here, and everytime i call the Action class, the service is injected, but MyRepository is alway null.

    could someone help me with please, if you need more infos, pls let me know, thx!

  2. #2
    Join Date
    Oct 2011
    Posts
    7

    Default

    define getter method and setter method for repo and then put @Autowired above the setter method and try.

  3. #3
    Join Date
    Jun 2012
    Posts
    5

    Default

    Hi prashob,

    thx for your comment! i just tried on my ServiceImpl class to add setter for the repo. It is still not working
    Code:
    public class ServiceImpl implements Server{
      MyRepository repo;
      
      @Autowired
      public void setRepo(MyRepository repo){
        this.repo = repo;
      }
    }

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    I doubt that the service gets injected and I suspect you are creating your own instance of the service (if @Autowired fails you would get an exception during startup!). In your configuration there is nothing that enables @Autowired or detecting your @Service (no context:component-scan).

    Also that Action what kind of class is that and how is that being constructed? If it isn't under spring control nothing is going to be injected...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Jun 2012
    Posts
    5

    Default

    Hello Marten,

    thx for your comment, the Action class is implemented with Struts2, i thought if i got

    <jpa:repositories base-package="com.myproject.repositories" />,

    i do not have to declare

    <context:component-scan base-package="com.myproject.repositories" />.

    i am think of that the action classes are not really under control of spring, i am not sure, how it works, maybe i do not need @Service at all, if it is not working.

    best, j

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Yuo don't need if for your repositories but for your @Service. Also I suggest you read the spring struts intregration stuff on the struts website. I also suggest the search as the questions regarding struts2 and spring have been answered numerous times before.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Jun 2012
    Posts
    5

    Default

    Hello Marten,

    thx for your comment, did you mean that i only need to declare <jpa:repositories /> and the base-package should point to my package, which contains the @Service. could i may be point the base-package to the parent package like "com.myproject"? because i have two packages for the services "com.myproject.service" for the interfaces and "com.myproject.service.impl" for the implementation.

    and Status UPDATE:

    i have added struts spring support yesterday, and it works well without @Service, i called MyRepository directly from my struts Action class.

    HOW can i add the service tier under control of spring. what kind of configuration do i need.

    appreciate your help.

    best, j

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •