2012-01-05 62 views
1

我有一個簡單的問題,當我調用一個託管bean方法返回一個列表時,爲什麼總是得到一個NPE。我在我看來使用了primefaces嚮導組件。例如可以sometone告訴我,這兩者之間的區別:調用託管bean方法返回一個NPE

不起作用:

public List<RequiredParam> getRequiredFields() { 
    if(!this.sdeCommand.getActions().isEmpty() &&this.action!=null &&!this.action.equals("")){ 
     for(CommandAction act:this.sdeCommand.getCommandActions()){ 
      if(act.getActionName().equalsIgnoreCase(this.action)){ 
       this.requiredFields.addAll(act.getFields()); 
      } 
     } 
    } 
    return this.requiredFields; 
} 

但是這個工程:

public List<RequiredParam> getRequiredFields() { 

    return this.requiredFields; 
} 

的觀點:

       <c:forEach items="${gdsiGeodataBean.requiredFields}" var="reqs"> 
            <h:outputLabel for="#{reqs.name}" value="#{reqs.name}:* " /> 
           </c:forEach> 

錯誤信息:

java.lang.NullPointerException 
    com.tsystems.appbeans.GdsiGeodataBean.getRequiredFields(GdsiGeodataBean.java:103) 
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    java.lang.reflect.Method.invoke(Method.java:597) 
    javax.el.BeanELResolver.getValue(BeanELResolver.java:62) 
    com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) 
    com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) 
    org.apache.el.parser.AstValue.getValue(AstValue.java:118) 
... 

我的觀點:

+0

顯然,你的一些bean的屬性是'null'。仔細檢查你是否已經正確實例化了所有的'List'屬性 – 2012-01-05 11:38:14

+0

@ Mr.J4mes,謝謝其中的一個屬性爲null。我正在使用一個嚮導,並且已經在前一個選項卡中設置了該屬性。不知何故該物業失去了價值。 – algone 2012-01-05 17:15:24

回答

1
this.sdeCommand.getActions().isEmpty() 

以上將拋出NPE如果getActions()返回null。首先檢查以確保getActions() != null。這可能是也可能不是你的問題,但它肯定是不安全的代碼,它不應該通過正式的代碼審查。

+0

謝謝你指出。我認爲這是問題所在。我確定該方法返回一個列表,但我應該檢查。現在工作正常!然而,我有另一個相關的問題,在同一個bean中,我有一個帶有getter和setter的簡單字符串屬性。吸氣劑工作正常,但不是裝置工。在setter方法中,我爲每個傳入參數構建一個Stringbuffer。那是錯的嗎?我已經在setter中進行了測試,看看是否傳入了輸入字符串,但顯然該方法根本沒有被調用/調用。在視圖中使用#{}表示法。 – algone 2012-01-05 17:12:30

+0

@algone請使用相關代碼更新您的代碼。 – 2012-01-05 17:45:26

+0

@algone如果沒有看到代碼,很難說出問題所在。請把它作爲一個單獨的問題發佈,我們可以嘗試幫助你。 – 2012-01-05 17:46:55

相關問題