Reputation: 1802
Hi I'm learning spring beans and I have tried out this simple bean. I have a class with a data member. data member is initialized using setter method. And I set the data member in my bean configuration xml with tag. I get a null pointer exception with reaspect to the "Message" that I'm trying to pass to the setter. I don't think NULL is actually being passed since I face the same issue when I passed a string literal.
This is my class:
public class HelloWorld implements DummyInterface {
//private WebAppPackagerPortal WebAppObj;
private String message;
public void setMessage(String message) {
this.message = message;
}
public String getMessage(){
return this.message;
}
@Override
public void printMessage() {
}
}
Here is my bean xml:
<bean id="HelloWorldBean" class="pkg.HelloWorld">
<property name="message" value="${content.msg}"/>
<!--<property name="message" value="DUMMYMSG"/>-->
</bean>
I'm getting an exception as follows:
> Feb 24, 2016 1:33:50 PM org.apache.catalina.core.StandardContext
> listenerStart SEVERE: Exception sending context initialized event to
> listener instance of class
> org.springframework.web.context.ContextLoaderListener
> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'HelloWorldBean' defined in ServletContext
> resource [/WEB-INF/spring-config.xml]: Error setting property values;
> nested exception is
> org.springframework.beans.PropertyBatchUpdateException; nested
> PropertyAccessExceptions (1) are: PropertyAccessException 1:
> org.springframework.beans.MethodInvocationException: Property
> 'message' threw exception; nested exception is
> java.lang.NullPointerException at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1514)
> at
...
Caused by:
> org.springframework.beans.PropertyBatchUpdateException; nested
> PropertyAccessExceptions (1) are: PropertyAccessException 1:
> org.springframework.beans.MethodInvocationException: Property
> 'message' threw exception; nested exception is
> java.lang.NullPointerException at
> org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:121)
> at
> org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
> at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1510)
> ... 26 more
Upvotes: 1
Views: 4100
Reputation: 184
There is nothing wrong with the code that you have posted. With spring the problem may not always be where you think it is :). Since you haven't posted the code where you are actually instantiating the bean or the spring version/jars you have used, I would suggest you try these steps.
<property name="message" value="${content.msg}"/>
from the xml and see if it works. If you get the same error then it means the problem is somewhere else. May be in the config.Second step would be to try run it outside the web container.
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("/spring-config.xml");
HelloWorld helloWorld = (HelloWorld ) applicationContext.getBean("HelloWorldBean");
If that worked then look at web application configuration and also compare the jars in the generated web application against build environment.
org.springframework.beans.PropertyBatchUpdateException; nested...
Sometimes incompatible jars cause this problem, though not always.
Upvotes: 1