Announcement Announcement Module
No announcement yet.
Nullpointer exception while using BeanPostProcessor Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Nullpointer exception while using BeanPostProcessor

    I am able to run the application fine if it is not configured with BeanPostProcessor in the container. As and when it is configured to run with BeanPostProcessor raising null pointer exception.

    Mar 24, 2013 9:19:26 PM ationContext prepareRefresh
    INFO: Refreshing plicationContext@766e3d60: startup date [Sun Mar 24 21:19:26 IST 2013]; root of context hierarchy
    Mar 24, 2013 9:19:26 PM org.springframework.beans.factory.xml.XmlBeanDefin itionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [BeanConfig.xml]
    Mar 24, 2013 9:19:26 PM istableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in istableBeanFactory@6542bece: defining beans [com.satya.spring.CustomBeanPostProcessor#0,employe e,customer]; root of factory hierarchy
    BEAN NAMEemployee
    postProcessBeforeInitializationcom.satya.spring.Em ployeeBean@7b5a6029
    Mar 24, 2013 9:19:26 PM ingletonBeanRegistry destroySingletons
    INFO: Destroying singletons in istableBeanFactory@6542bece: defining beans [com.satya.spring.CustomBeanPostProcessor#0,employe e,customer]; root of factory hierarchy
    Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'employee' defined in class path resource [BeanConfig.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
    at AutowireCapableBeanFactory.initializeBean(Abstract
    at AutowireCapableBeanFactory.doCreateBean(AbstractAu
    at AutowireCapableBeanFactory.createBean(AbstractAuto
    at BeanFactory$1.getObject( 91)
    at ingletonBeanRegistry.getSingleton(DefaultSingleton
    at BeanFactory.doGetBean( )
    at BeanFactory.getBean(
    at istableBeanFactory.preInstantiateSingletons(Defaul
    at ationContext.finishBeanFactoryInitialization(Abstr
    at ationContext.refresh(AbstractApplicationContext.ja va:425)
    at plicationContext.<init>(ClassPathXmlApplicationCon
    at plicationContext.<init>(ClassPathXmlApplicationCon
    at com.satya.spring.Client.main(
    Caused by: java.lang.NullPointerException
    at AutowireCapableBeanFactory.invokeCustomInitMethod(
    at AutowireCapableBeanFactory.invokeInitMethods(Abstr
    at AutowireCapableBeanFactory.initializeBean(Abstract
    ... 12 more

    package com.satya.spring;

    import org.springframework.beans.factory.BeanNameAware;
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean ;

    public class EmployeeBean implements InitializingBean,DisposableBean,BeanNameAware{
    private String empName;

    public String getEmpName() {
    System.out.println("GETTING EMP NAME"+empName);
    return empName;

    public void setEmpName(String empName) {
    this.empName = empName;
    System.out.println("EMP NAME SET");

    public void afterPropertiesSet() throws Exception {
    // TODO Auto-generated method stub

    public void destroy() throws Exception {
    System.out.println("DESTROY METHOD HAS BEEN CALLED");
    // TODO Auto-generated method stub
    public void myInit(){
    System.out.println("MY INIT METHOD EXECUTION");

    public void myDestroy(){
    System.out.println("MY DESTROY METHOD EXECUTION");

    public void setBeanName(String arg0) {
    // TODO Auto-generated method stub
    System.out.println("BEAN NAME"+arg0);



    package com.satya.spring;

    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostP rocessor;

    public class CustomBeanPostProcessor implements BeanPostProcessor {

    public Object postProcessAfterInitialization(Object arg0, String arg1)
    throws BeansException {
    // TODO Auto-generated method stub
    System.out.println("postProcessAfterInitialization ");
    return null;


    public Object postProcessBeforeInitialization(Object arg0, String arg1)
    throws BeansException {
    // TODO Auto-generated method stub
    System.out.println("postProcessBeforeInitializatio n"+arg0.toString());
    return null;



    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns=""
    <bean class="com.satya.spring.CustomBeanPostProcessor"/>

    <bean id="employee" class="com.satya.spring.EmployeeBean" init-method="myInit" destroy-method="myDestroy">
    <property name="empName" value="satya"></property>

    package com.satya.spring;

    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFacto ry;
    import org.springframework.context.ApplicationContext;
    import plicationContext;

    public class Client {
    public static void main(String[]args){
    ApplicationContext ctx = new ClassPathXmlApplicationContext("BeanConfig.xml");
    //BeanFactory factory = new XmlBeanFactory(new ClassPathResource("BeanConfig.xml"));

    Please help me how to resolve this error.

  • #2
    Please use [ code][/code ] tags when posting code/xml/stacktraces, that way your code remains readable, currently it is hard to decipher...

    Your BeanPostProcessor is destroying the beans, you are returning null all over the place meaning you are overriding beans with null... I suggest you start to read the documentation on what a Bean(Factory)PostProcessor does and how it works.