[英]Spring bean creation with setter method - null pointer exception
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.
数据成员使用setter方法初始化。 And I set the data member in my bean configuration xml with tag.
然后,在Bean配置xml中使用标记设置数据成员。 I get a null pointer exception with reaspect to the "Message" that I'm trying to pass to the setter.
关于尝试传递给setter的“消息”,我得到了一个空指针异常。 I don't think NULL is actually being passed since I face the same issue when I passed a string literal.
我不认为实际上传递了NULL,因为在传递字符串文字时遇到相同的问题。
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 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
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.
由于您尚未在实际实例化所使用的bean或spring版本/ jar的地方发布代码,因此建议您尝试以下步骤。
<property name="message" value="${content.msg}"/>
from the xml and see if it works. <property name="message" value="${content.msg}"/>
,并查看其是否有效。 If you get the same error then it means the problem is somewhere else. Second step would be to try run it outside the web container. 第二步是尝试在Web容器之外运行它。
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. 如果可行,请查看Web应用程序配置,并将生成的Web应用程序中的jar与构建环境进行比较。
org.springframework.beans.PropertyBatchUpdateException;
org.springframework.beans.PropertyBatchUpdateException; nested...
嵌套...
Sometimes incompatible jars cause this problem, though not always. 有时不兼容的jar会导致此问题,尽管并非总是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.