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

  • 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
    define getter method and setter method for repo and then put @Autowired above the setter method and try.

    Comment


    • #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
      Code:
      public class ServiceImpl implements Server{
        MyRepository repo;
        
        @Autowired
        public void setRepo(MyRepository repo){
          this.repo = repo;
        }
      }

      Comment


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

        Comment


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

          Comment


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

            Comment


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

              Comment

              Working...
              X