Announcement Announcement Module
No announcement yet.
NullPointerException from DAO by using crudRepositoy Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • NullPointerException from DAO by using crudRepositoy


    i got java.lang.NullPointerException by using CrudRepository

    my project looks like this:

    strut2 action

    class Action {
      @Autowired MyService service;
      public String execute(){
        return "ok";
    interface Service {
      List getList();
    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:

      <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
        <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" />
        <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 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://" />
        <bean class=""></bean>  
        <tx:annotation-driven transaction-manager="transactionManager" />
    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
    define getter method and setter method for repo and then put @Autowired above the setter method and try.


    • #3
      Hi prashob,

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


      • #4
        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...


        • #5
          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
            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.


            • #7
              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