1

I am getting Null Pointer Exception during server startup in the first line.

public class DefaultAreaPostalCodeService extends AbstractBusinessService implements AreaPostalCodeService {

        private Map<String,List<PostalCodeData>> suburbMap;

        @PostConstruct
        @Transactional
        public void initialize() {
            List<AreaPostalCodeModel> postalCodes = areaPostalCodeDao.getAllAreaPostalCodes();
            populateSuburbMap(postalCodes);
        }
    }

<bean id="areaPostalCodeService"
          class="za.co.testro.core.address.impl.DefaultAreaPostalCodeService" parent="abstractBusinessService">
        <property name="areaPostalCodeDao" ref="areaPostalCodeDao" />
    </bean>

I simply want to populate the suburbMap at server startup so that I can use it later.

Error logs-

Error creating bean with name 'areaPostalCodeService': Invocation of init method failed; nested exception is java.lang.NullPointerException WARN [localhost-startStop-1] [CloseAwareApplicationContext] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'areaPostalCodeService': Invocation of init method failed; nested exception is java.lang.NullPointerException Pinging the JVM took 10 seconds to respond. ERROR [localhost-startStop-1] [HybrisContextFactory] Error initializing global application context! org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'areaPostalCodeService': Invocation of init method failed; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136) ~[spring-beans-4.3.3.REL

EDIT 1:

I am still getting Null Pointer Exception after adding my code to afterProperties().

INFO [localhost-startStop-1] [ListMergeDirectiveBeanPostProcessor] Post Processing ListMergeDirective [promotionActionResultRaoExtractorListMergeDirective] on Bean [cartRAOProviderExtractors] WARN [localhost-startStop-1] [CloseAwareApplicationContext] Exception encoujava.lang.NullPointerException WARN [localhost-startStop-1] [CloseAwareApplicationContext] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'areaPostalCodeService' defined in class path resource [testcore-spring.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException ERROR [localhost-startStop-1] [HybrisContextFactory] Error initializing global application context! org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'areaPostalCodeService' defined in class path resource [testcore-spring.xml]: Invocation of init method failed; ne sted exception is java.lang.NullPointerException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]ntered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'areaPostalCodeService' defined in class path resource [testcore-spring.xml]: Invocation of init method failed; nested exception is

EDIT 2:

Still getting Null Pointer Exception after calling the bean with application context.

Registry.getApplicationContext().getBean("areaPostalCodeDao", AreaPostalCodeDao.class).getAllAreaPostalCodes()

Error logs-

INFO [localhost-startStop-1] [ListMergeDirectiveBeanPostProcessor] Post Processing ListMergeDirective [promotionActionResultRaoExtractorListMergeDirective] on Bean [cartRAOProviderExtractors] WARN [localhost-startStop-1] [CloseAwareApplicationContext] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'areaPostalCodeService' defined in class path resource [testcore-spring.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException WARN [localhost-startStop-1] [CloseAwareApplicationContext] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'areaPostalCodeService' defined in class path resource [testcore-spring.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException ERROR [localhost-startStop-1] [HybrisContextFactory] Error initializing global application context! org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'areaPostalCodeService' defined in class path resource [testcore-spring.xml]: Invocation of init method failed; ne sted exception is java.lang.NullPointerException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]

Caused by: org.springframework.beans.FatalBeanException: Context hybris Global Context Factory couldn't be created correctly due to, Error creating bean with name 'areaPostalCodeService' defined in class path resource [testcore-spring.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException at de.hybris.platform.core.HybrisContextFactory.build(HybrisContextFactory.java:314)

Farrukh Chishti
  • 7,652
  • 10
  • 36
  • 60
  • 1
    `BeanCreationException` , post `DefaultAreaPostalCodeService` code – gvmani Jul 03 '18 at 14:15
  • 1
    Try without `@Transactional` annotation. If it works, change your class to a child class of `InitializingBean` and use `@AfterPropertiesSet` annotation instead of `@PostConstruct` as setters injections aren't called in post-construct yet. – Edward Aung Jul 03 '18 at 14:32
  • 1
    The NPE is likely bubbling up from your initialize method, surround that code with a try catch and see what's up. You're missing some code here that could be helpful, e.g. the contents of the populateSuburbMap method so this is purely speculation, but are you perhaps trying to add to suburbMap before having constructed it? – lane.maxwell Jul 03 '18 at 19:02

3 Answers3

1

it is very normal to have such behavior, because @PostConstruct method will be called before injecting areaPostalCodeDao bean into areaPostalCodeDao property wich is null (for that moment).

There are 4 ways to run some code on bean initializing, but each way succeed in a specific step. Spring bean setup lifecycle:

  1. standard constructor
  2. @PostConstruct
  3. @Override afterPropertiesSet() from InitializingBean interface
  4. init-method

So in your case you should do as above :

import org.springframework.beans.factory.InitializingBean;  

public class DefaultAreaPostalCodeService extends AbstractBusinessService implements AreaPostalCodeService,InitializingBean {

    private Map<String,List<PostalCodeData>> suburbMap;

    @Override
    @Transactional
    public void afterPropertiesSet() {
        List<AreaPostalCodeModel> postalCodes = areaPostalCodeDao.getAllAreaPostalCodes();
        populateSuburbMap(postalCodes);
    }
}
Andrew
  • 95
  • 2
  • 12
Mohamed Nabli
  • 1,629
  • 3
  • 17
  • 24
  • I tried this but I am still getting the Null Pointer Exception. – Farrukh Chishti Jul 04 '18 at 09:47
  • did you try other init-method solution ?? – Mohamed Nabli Jul 04 '18 at 12:54
  • honestly i have tried everything...ranging from init-method to constructor-arg to @Postconstruct but I am getting Null Pointer Exception everytime and that too in some JVM files. I tried activating my bean after server start-up and that is working perfectly fine. So, I assume there is something wrong with the method I am using to initialize the bean at server startup. – Farrukh Chishti Jul 04 '18 at 12:58
  • 1
    try this : `ApplicationContextProvider.getApplicationContext().getBean("areaPostalCodeDao", AreaPostalCodeDao.class).getAllAreaPostalCodes();` – Mohamed Nabli Jul 04 '18 at 13:04
  • 1
    did you check if `suburbMap` is initialized ?? – Mohamed Nabli Jul 04 '18 at 13:34
  • I am testing it now. The build and server startup takes a lot of time !!! Also, I can't access ApplicationContextProvider in my storefront module so I am using Registry. – Farrukh Chishti Jul 04 '18 at 13:45
  • Still the same Null Pointer Exception. I have uploaded the logs in original question in EDIT 2. – Farrukh Chishti Jul 04 '18 at 14:15
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174391/discussion-between-mohamed-nabli-and-farrukh-chishti). – Mohamed Nabli Jul 05 '18 at 08:13
  • This not true - Beans are already injected at this point. – akop Aug 03 '23 at 14:03
0

A possible solution, maybe not specifically for this case but could help others, is to call the code from the view, call it on first glance. The code would be something like:

In XHTML

<f:metadata>
     <f:viewAction action="#{myUI.myVoid()}"/>
</f:metadata>
0

Are you sure, that areaPostalCodeDao is the cause of the NPE?
I guess, that there is something wrong inside your initialize() method.

Please read: What is a NullPointerException, and how do I fix it?

Unlike Mohamed Nabli wrote, Beans are initialised when @PostConstruct is called. Simple google research:

akop
  • 5,981
  • 6
  • 24
  • 51