Results 1 to 3 of 3

Thread: Spring DI => NPE

  1. #1
    Join Date
    Dec 2010
    Posts
    2

    Default Spring DI => NPE

    Hi

    I have a problem with class ImportUserFromExternalSourceAD, userDAO cant be injected, i getting NPE
    Here is a details (i hope) description of my problem:

    Code:
    package org.system.server;
    
    
    @Service("UserService")
    public class  UserServiceImpl extends RemoteServiceServlet implements UserService {
    @Autowired ImportUserFactory importUserFactory;
    
    @Override
    				public List<BaseModel> getUsersFromExternalStore(AuthmethodDTO storeType) {
         return importUserFactory.get(authmethod).get();
    }
    
    }



    Code:
    package org.system.server.dao;
    
    
    public abstract class ImportUserFactory {
    	public abstract ImportUserFromExternalSource get(Authmethod authmethod);
    }
    Code:
    package org.system.server.dao.impl;
    
    i
    
    @Repository
    public class ImportUserFactoryImpl extends ImportUserFactory{
    
    	
    	
    	@Override
    	public ImportUserFromExternalSource get(Authmethod authmethod) {
    		
    		if(authmethod.getType().equals("ad")){
    			ImportUserFromExternalSource source = new ImportUserFromExternalSourceAD();
    			source.setAuthmethod(authmethod);
    			return source;
    		}
    		
    		
    		return null;
    	}
    
    }
    Code:
    package org.system.server.dao;
    
    public interface ImportUserFromExternalSource {
    	
    	public void setAuthmethod(Authmethod authmethod);
    	public List<User> get();
    
    }

    So here i get NPE in userDAO.listUser("any");

    Code:
    package org.system.server.dao.impl;
    
    @Repository
    public class ImportUserFromExternalSourceAD implements ImportUserFromExternalSource{
    	@Autowired
    	private UserDAO userDAO;
    
    @Override
    	public List<User> get() {
    for(User u: userDAO.listUser("any"))
    			System.out.println(u.getFirstName());
    
    }
    
    }

    applicationContext.xml

    Code:
    <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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
    
        <context:component-scan base-package="org.system.server" />
    	
    	
    	<context:annotation-config/>
    
    	<tx:annotation-driven transaction-manager="transactionManager" />
    
    <!-- i have remove all about hibernate here-->
    
    </beans>
    In other classes everything's ok so i think the problem in ImportUserFactoryImpl where i return source:

    ImportUserFromExternalSource source = new ImportUserFromExternalSourceAD();

    Thanks for any help

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    You aren't using spring DI.. Well you are defining the beans but next you create new instances yourself, you must use the instance from the context not your own instances.
    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

  3. #3

    Default

    Hi,
    I think it ll better and clear too if you will use xml mapping without using annotation.

    spring framework
    Last edited by abani; Dec 18th, 2011 at 01:21 AM.

Posting Permissions

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