0

Here, all the dependencies in JobRepository are autowired correctly. But in the init() method, when jobUtil.findRootCarrier() is called, carrierService inside JobUtil seems to be null and its causing NPE.

@Component
@Transactional(Transactional.TxType.REQUIRES_NEW)
public class JobRepository {

    @Autowired
    public JobUtil jobUtil;

    @Autowired
    public JobsInitializer jobsInitializer;

    @PostConstruct
    public void init() {
        Form form = getBatchJobsForm();
        .........
    }

    private synchronized Form getBatchJobsForm() {
         Carrier carrier = jobUtil.findRootCarrier();
         .........
    }

}


@Component
public class JobUtil {

        @Autowired
        public CarrierService carrierService;                        

        public Carrier findRootCarrier() {
            List<Carrier> topLevelCarriers = carrierService.findTopLevelCarriers(); // carrierService is null
        }
}

I assumed PostConstruct gets called after all the dependencies including nested ones gets injected. Am I wrong?

Edit

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobRepository': Invocation of init method failed; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1626)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:518)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:496)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:627)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:318)
    ... 575 more
Caused by: java.lang.NullPointerException
    at com.****.util.JobUtil.findRootCarrier(JobUtil.java:161)
    at com.****.scheduler.jobs.JobRepository.getBatchJobsForm(JobRepository.java:137)
    at com.****.scheduler.jobs.JobRepository.init(JobRepository.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)
    ... 592 more
Drunken Daddy
  • 7,326
  • 14
  • 70
  • 104
  • `CarrierService` is `@Autowired` to `JobUtil`. If `jobUtil.carrierService` were `null`, the `JobRepository::init` shouldn't even run I think. – narendra-choudhary Apr 29 '20 at 08:44
  • 1
    What is the exact error message returned by program? Can you add detailed error with relevant part of stack trace? – narendra-choudhary Apr 29 '20 at 08:46
  • @narendra-choudhary I've edit my question and added the error. I was under the same impression as you. the JobRepository::init shouldn't even run I think. But now I am confused – Drunken Daddy Apr 29 '20 at 09:14
  • Tragically PostConstruct's can have half instantiated objects when there are circular dependencies: https://stackoverflow.com/a/64649202/32453 – rogerdpack Nov 02 '20 at 18:40

0 Answers0