1
votes

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)

2
BeanCreationException , post DefaultAreaPostalCodeService codegvmani
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
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

2 Answers

2
votes

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);
    }
}
0
votes

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>