1

簡単な質問があります。リストを返すマネージドBeanメソッドを呼び出すと、なぜ常にNPEを取得するのですか。ビューでprimefacesウィザードコンポーネントを使用しています。たとえば、sometoneは、これら2つの違いを教えてくれます。

動作しません:

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)
...

私の見解:

4

1 に答える 1

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

getActions()null を返す場合、上記は NPE をスローします。最初に確認してくださいgetActions() != null。これはあなたの問題かもしれませんし、そうでないかもしれませんが、これは確かに安全でないコードであり、正式なコード レビューに合格するべきではありません。

于 2012-01-05T12:23:34.640 に答える