2

I'm using grails 2.3.1 and I have two services.

ServiceA implements InitializingBean, SomeInterface {

    def grailsApplication
    List someList

    @Override
    void afterPropertiesSet() throws Exception {
        // initialization of someList using grailsApplication
    }

    @Override
    void methodFromSomeInterface() { }

}

and the second service, does almost similar thing, differently

ServiceB extends ServiceA {

    // no need for the member variables, they'll be inherited
    // def grailsApplication
    // List someList

    // should not need the afterPropertiesSet as well, it'll be inhereted
    // but that fails (same error as below), so the following change was made
    @Override
    void afterPropertiesSet() throws Exception {
        super.afterPropertiesSet();
    }

    @Override
    void methodFromSomeInterface() { }

}

Now, it throws an NPE

org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'someController':
Initialization of bean failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'serviceB':
Invocation of init method failed;
nested exception is java.lang.NullPointerException
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'serviceB':
Invocation of init method failed;
nested exception is java.lang.NullPointerException

What am I doing wrong here?

Btw, it works with the following code...

ServiceA implements InitializingBean, SomeInterface {

    def grailsApplication
    List someList

    @Override
    void afterPropertiesSet() throws Exception { init() }

    protected void init() { 
        // initialization of someList using grailsApplication
    }

    @Override
    void methodFromSomeInterface() { }

}

and the second service

ServiceB extends ServiceA {

    // no need for the member variables, they'll be inherited
    // def grailsApplication
    // List someList

    @Override
    void afterPropertiesSet() throws Exception { init() }

    @Override
    void methodFromSomeInterface() { }

}

This works! but why not the first one?

bdas
  • 63
  • 1
  • 6

0 Answers0