1

I have a simple question, why do i always get a NPE when I invoke a managed bean method that either returns a list. I am using primefaces wizard component in my view. For instance can sometone tells me the difference between these two:

Does not work:

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;
}

However this Works:

public List<RequiredParam> getRequiredFields() {

    return this.requiredFields;
}

The view:

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

Error message:

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

my view:

algone
  • 93
  • 1
  • 2
  • 9
  • Obviously, some of your bean's properties are `null`. Check carefully if you have instantiate all your `List` properties properly – Mr.J4mes Jan 05 '12 at 11:38
  • @ Mr.J4mes, Thanks in deed one of the properties was null. I am using a wizard, and I had set bound the property in a previous tab. somehow the property lost the value. – algone Jan 05 '12 at 17:15

1 Answers1

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

The above will throw an NPE if getActions() returns null. Check to make sure that getActions() != null first. This may or may not be your problem but it certainly is unsafe code and it should never pass a formal code review.

maple_shaft
  • 10,435
  • 6
  • 46
  • 74
  • Thanks for pointing that out. I think that was the problem. I was sure the method returns a list, but I should have checked. works fine now! However, I have another related problem, in the same bean I have a simple string property with a getter and setter. The getter works fine, but not the setter. In the setter method I am building a Stringbuffer for each of the incoming parameter. Is that wrong? I have tested within the setter to see if the input string is being passed but apparently the method is not being called/invoked at all. In the view am using a #{} notation. – algone Jan 05 '12 at 17:12
  • @algone It is hard to say what the problem is without seeing code. Please post this as a seperate question and we can try to help you. – maple_shaft Jan 05 '12 at 17:46
  • @Mr.J4mes It is a seperate question and should be posted in a different question, not in this question. – maple_shaft Jan 05 '12 at 17:47
  • @Mr.J4mes The Question has been posted now see:[link](http://stackoverflow.com/questions/8756102/bean-properties-not-being-updated) – algone Jan 06 '12 at 10:05
  • @maple_shaft The Question has been posted now see: [bean-properties-not-being-updated](http://stackoverflow.com/questions/8756102/bean-properties-not-being-updated) – algone Jan 06 '12 at 10:07