簡體   English   中英

使用setter方法創建Spring bean-空指針異常

[英]Spring bean creation with setter method - null pointer exception

嗨,我正在學習春季豆,我已經嘗試了這個簡單的豆。 我有一個帶有數據成員的課程。 數據成員使用setter方法初始化。 然后,在Bean配置xml中使用標記設置數據成員。 關於嘗試傳遞給setter的“消息”,我得到了一個空指針異常。 我不認為實際上傳遞了NULL,因為在傳遞字符串文字時遇到相同的問題。

這是我的課:

 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() {
    }
}

這是我的bean xml:

<bean id="HelloWorldBean" class="pkg.HelloWorld">
    <property name="message" value="${content.msg}"/>
    <!--<property name="message" value="DUMMYMSG"/>-->
</bean>

我收到如下異常:

> 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

您發布的代碼沒有任何問題。 在春季,問題可能並不總是出現在您認為的地方:)。 由於您尚未在實際實例化所使用的bean或spring版本/ jar的地方發布代碼,因此建議您嘗試以下步驟。

  1. 從xml中刪除<property name="message" value="${content.msg}"/> ,並查看其是否有效。 如果您遇到相同的錯誤,則說明問題出在其他地方。 可能在配置中。
  2. 第二步是嘗試在Web容器之外運行它。

     ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-config.xml"); HelloWorld helloWorld = (HelloWorld ) applicationContext.getBean("HelloWorldBean"); 
  3. 如果可行,請查看Web應用程序配置,並將生成的Web應用程序中的jar與構建環境進行比較。

  4. 如果那不起作用,那么您的設置中可能有不兼容的jar。

    org.springframework.beans.PropertyBatchUpdateException; 嵌套...

有時不兼容的jar會導致此問題,盡管並非總是如此。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM