Reputation: 1896
Given this XML Spring code below:
<bean id="baseCarFacade" class="com.foo.BaseCarFacade">
<property name="engineFacade" ref="engineFacade" />
</bean>
<bean id="engineFacade" class="com.foo.EngineFacade" />
<bean id="childCarFacade" class="com.foo.ChildCarFacade" parent="baseCarFacade" />
and this Java Code:
public class BaseCarFacade implements IBaseMemberFacade {
public void setEngineFacade(EngineFacade engineFacade) {
this.engineFacade = engineFacade;
}
...
}
public class EngineFacade {
...
}
public class ChildCarFacade extends BaseCarFacade {
public void doSomethingSpecial() {
someObject = engineFacade.doSomethingReallySpecial();
}
...
}
I have a property that I'd like to inject (engineFacade) in a child class (childCarFacade) that could potentially be injected for other children of the parent class (BaseCarFacade). Instead of redundantly having this property in all of the child classes I was hoping to have it at the parent level and then simply use it in any of the children as shown above where I'm calling "doSomethingReallySpecial()".
However, whenever I execute this code I'm getting a runtime null pointer exception that looks like this:
Caused by: java.lang.NullPointerException at com.foo.EngineFacade.doSomethingReallySpecial(EngineFacade.java:48)
Here is a line from the instantiation of my beans:
Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@32273227: defining beans [winkInternalLifecycleManager,winkInternalLifecycleManagersRegistry,winkInternalRegistryPostProcessor,winkInternalLifecycleManagerPostProcessor,winkInternalDependenciesInjectionPostProcessor,winkInternalDeploymentConfiguration,winkInternalRequestProcessorBuilder,winkInternalDefaultPropertiesFactory,winkInternalPropertiesFactory,winkInternalPropertyPlaceholderConfigurer,winkInternalNullContant,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.apache.wink.spring.Registrar#0,jaxbProvider,jacksonObjectMapper,jacksonAnnotationIntrospector,primaryAnnotationIntrospector,secondaryAnnotationIntrospector,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,propertyPlaceholderConfigurer,allProperties,dataSource,dynacache,commonCache,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,jdbcTemplate,baseCarFacade,engineFacade,childCarFacade, ...
You can see the last ones seem to be instantiated correctly.
Could someone point out the error of my ways? I feel like this should be simple...
Upvotes: 0
Views: 1147
Reputation: 1896
Well, this is extremely embarrassing but as it turns out I was looking a level too deep at the Null Pointer Exception. Essentially there was an NPE within the doSomethingReallySpecial() method, not within the engineFacade at all. The engineFacade was being set correctly the entire time. What that means is that the original code I posted above (as Eric. B mentioned) does indeed work correctly.
I apologize for missing something so simple; hopefully in the end someone else will find this question helpful nonetheless..
Thanks for your help Eric. B and user1030367!
Upvotes: 1
Reputation: 166
Maybe try exposing a getter in the parent and to getEngineFacade().doSomethingReallySpecial()?
Upvotes: 1