Question

I run here to a very specific Java Spring issue I can not understand. I have created myself an abstract class, f.e:

public abstract class AbstractCrudServiceImpl{

    private GenericDAO baseDao;

    public GenericDAO getBaseDao() {
     return baseDao;
    } 
}

Generic Dao is an interface. I wanted to have the getter (and setter) in the abstract class, so the implementation would be rather simple and clear. [Does not matter if its not atm].

Then have it's implementation, for instance:

public class AgentServiceImpl extends AbstractCrudServiceImpl{

    @Autowired
    private AgentDao baseDao;
}

Using getter of superclass to run all the methods.

My intention was to have a similar effect as if I use XML like:

<bean id="AbstractCrudServiceImpl" class="..." abstract="true" />

<bean id="AgentServiceImpl" parent="AbstractCrudServiceImpl">
    <property name="baseDao" ref="agentDao"/>
</bean>

And to my surprise I got an error saying:

java.lang.NullPointerException
 com.insurance.central.services.impl.AbstractCrudServiceImpl.read(AbstractCrudServiceImpl.java:36)

which basically means, that the property is not set properly, because it's value is still null. Why!?

How should I correct it.

Thanks.

Was it helpful?

Solution

Spring is setting correctly the field baseDao in AgentServiceImpl. The field baseDao in parent class AbstractCrudServiceImpl stays null. In Java you can override a method, not a member variable. Try AgentServiceImpl.baseDao vs. AgentServiceImpl.super.baseDao.

OTHER TIPS

I don't know much about Spring, so I can't speak to the annotations or anything, but your original property is private, so you can't override it. Try changing both to protected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top