Patty Patty - 5 months ago 152
Java Question

Java-Spring: NullPointerException during context initialization (Using @Component, @Autowired, , context:component-scan)

I am new to spring and seeing a NullPointerException during context initialization. Here is the error msg:

10:23:43,231 WARN [ClassPathXmlApplicationContext] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'RoxourProcessor' defined in file [C:\my_sand\branch\Roxour-client-api\target\classes\company\online\Roxour\api\batchOperations\RoxourProcessor.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [company.online.Roxour.api.batchOperations.RoxourProcessor]: Constructor threw exception; nested exception is java.lang.NullPointerException
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'RoxourProcessor' defined in file [C:\my_sand\branch\Roxour-client-api\target\classes\company\online\Roxour\api\operation\RoxourProcessor.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [company.online.Roxour.api.batchOperations.RoxourProcessor]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at company.online.Roxour.api.RoxourMasterProcessor.main(RoxourMasterProcessor.java:170)


Here in my context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">


<!-- Scans within the base package of the application for @Components to configure as beans -->
<context:component-scan base-package="org.online.Roxour.api"/>




Here is the main class:

package org.online.Roxour.api;

public class RoxourMasterProcessor
{
......

@Autowired
RoxourProcessor processor;

........

public void process()
{
.......
processor.runJob();
...........
}

public static void main(String args[])
{

ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
LOG.info("Context: " + context);
RoxourMasterProcessor master = context.getBean( RoxourMasterProcessor.class );

master.process();

}
}


in a subpackage org.online.Roxour.api.utils, I have ApplicationEnvironment and RoxourUtil classes. Like:

package org.online.Roxour.api.utils;

@Component
public class ApplicationEnvironment
{
public ApplicationEnvironment()
{
Properties properties = new Properties();
try
{
LOG.info("Initiating Application Environment");
properties.load( this.getClass().getClassLoader().getResourceAsStream( "application.properties" ) );
load( properties );
}
catch (Throwable e)
{
String msg = "Error loading environment config.";
LOG.error(msg,e);

System.exit(1);
}
}
}


and

package org.online.Roxour.api.utils;

@Component
@Scope("prototype")
public class RoxourUtil
{
........

@Autowired
ApplicationEnvironment applicationEnvironment;

.........

}


In another subpackage org.online.Roxour.api.operation, I have class RoxourProcessor:

package org.online.Roxour.api.operation;

@Component
@Scope("singleton")
public class RoxourProcessor
{

.........

private int PAYLOAD_SIZE;

private int MAX_READER_THREADS;
private int MAX_WORKER_THREADS;

@Autowired
ApplicationEnvironment applicationEnvironment;

@Autowired
RoxourUtil util;

public RoxourProcessor()
{
PAYLOAD_SIZE = applicationEnvironment.getPayloadSize();
MAX_READER_THREADS = applicationEnvironment.getMaxReaderThreads();
MAX_WORKER_THREADS = applicationEnvironment.getMaxWorkerThreads();
}

..............

}


During the context initialization , I am seeing :

@Autowired

ApplicationEnvironment applicationEnvironment;

@Autowired

RoxourUtil util;

are null. Hence getting NULLPointerException from RoxourProcessor-constructor.

But not understanding how it did not find out those 2 autowired. What am I missing?

Any help is appreciated.

Thanks

Answer

Field injection happens after the constructor call. Replace the constructor with a @PostConstruct method and you should be fine.

@PostConstruct public void initialize(){
    PAYLOAD_SIZE = applicationEnvironment.getPayloadSize();
    MAX_READER_THREADS = applicationEnvironment.getMaxReaderThreads();
    MAX_WORKER_THREADS = applicationEnvironment.getMaxWorkerThreads();
}

This is one of the many reasons why field injection (or setter injection, for that matter) is considered a bad idea these days. You should prefer constructor injection, it makes your services immutable and test-friendly, and you won't run into race conditions during initialization.