0

I am trying to Inject a seam component into another, auto creating it. But for some reason the injected seam component throws NPE.

XHTML

                   <a4j:commandLink id="cbrModal"
                                     action="#{detailAction.showInformation(1L)}"
                                     reRender="DetailModal"
                                     limitToList="true">
                        <h:outputText value="text"/>

                    </a4j:commandLink>

DetailActionBean.java

@Name("detailAction")
public class DetailActionBean implements Serializable {

    @In(create = true, required = false)
    @Out(required = false)
    private RulesValidator rulesValidator;

   public void showInformation(long id) {

                rulesValidator.setCheckCount(0); // rulesValidator == null here and throws npe

   }
)

RulesValidator.java

@AutoCreate
@Name("rulesValidator")
@Scope(ScopeType.SESSION)
public class RulesValidator implements Serializable {

    private int checkCount = 0;
    public void setCheckCount(int checkCount) {
        this.checkCount = checkCount;
    }


}
Himalay Majumdar
  • 3,883
  • 14
  • 65
  • 94

2 Answers2

1

Required false means exactly that. If it does not exist yet it will not be created and you should check that. Autocreate just means that you dont need to define create true on the in annotation.

Update for the comments: Yes seam will autocreate a component on injection if this annotation is presemt. But you state in the injection that it is not required! Thats why seam does nothing. Just remove all properties of your @In and it should work. The defaults are what you want.

Martin Frey
  • 10,025
  • 4
  • 25
  • 30
  • Seam docs say, AutoCreate Specifies that this component should be automatically instantiated whenever it is asked for, even if @In does not specify create=true. Do you mean I need to `@Create public void init() { rulesValidator = new rulesValidator(); }` If I do this my rulesValidator instance variables getting initialized every time I call them. – Himalay Majumdar Feb 15 '12 at 14:43
  • Just to clarify, I want `rulesValidator.setCheckCount(0);` to execute, having an NPE check their would just bypass the call. – Himalay Majumdar Feb 15 '12 at 14:57
1

Seam will scan a base package and look for @Name components and then those components are auto wireable. I am supposed to put a seam.properties file (empty) for seam to know which base packages to scan. The module I was working on dint have seam.properties and so RulesValidator was not being scanned and treated as a seam component. Hence autoCreate dint work.

Himalay Majumdar
  • 3,883
  • 14
  • 65
  • 94