title: JSF2 Managed Bean variable initialization
body: <p>I'm having some problems with bean initialization. I have an "edit" form to update 
  some user data. The user is previously created in the database and 
  I retrieve successfuly the data and put it in the form. </p>
  
  <p>This is the form with id parameter 1 (/cms/admin/users/edit.jsf?id=1):</p>
  
  <pre><code>&lt;h:panelGrid styleClass="general-form" columns="3" id="ajaxForm"&gt;
  &lt;h:outputLabel for="name" value="#{cms['users.add.name']}" /&gt;
  &lt;p:inputText required="true" id="name"
      value="#{editUserController.user.name}"
      label="#{cms['users.add.name']}"&gt;
      &lt;f:validateLength minimum="3" /&gt;
  &lt;/p:inputText&gt;
  &lt;p:message for="name" /&gt;
  
  &lt;h:outputLabel value="#{cms['users.add.lastname']}" /&gt;
  &lt;p:inputText id="lastname" label="#{cms['users.add.lastname']}"
      required="true" value="#{editUserController.user.lastname}"&gt;
      &lt;f:validateLength minimum="3" /&gt;
  &lt;/p:inputText&gt;
  &lt;p:message for="lastname" /&gt;
  
  &lt;h:outputLabel value="#{cms['users.add.username']}" /&gt;
  &lt;p:inputText id="username" required="true"
      label="#{cms['users.add.username']}"
      value="#{editUserController.user.username}"&gt;
      &lt;f:validateLength minimum="3" /&gt;
  &lt;/p:inputText&gt;
  
  &lt;p:message for="username" /&gt;
  
  &lt;h:outputLabel value="#{cms['users.add.password']}" /&gt;
  &lt;p:password id="password" required="true"
      value="#{editUserController.user.password}"
      promptLabel="#{cms['users.error.enterPassword']}"
      weakLabel="#{cms['users.error.weakPassword']}"
      goodLabel="#{cms['users.error.goodPassword']}"
      strongLabel="#{cms['users.error.strongPassword']}"
      binding="#{password}" label="#{cms['users.add.password']}"&gt;
      &lt;f:validateLength minimum="6" /&gt;
  &lt;/p:password&gt;
  &lt;p:message for="password" /&gt;
  
  &lt;h:outputLabel value="#{cms['users.add.confirmPassword']}" /&gt;
  &lt;p:password id="passwordConfirm" required="true"
      value="#{editUserController.confirmPassword}"
      promptLabel="#{cms['users.error.enterPassword']}"
      weakLabel="#{cms['users.error.weakPassword']}"
      goodLabel="#{cms['users.error.goodPassword']}"
      strongLabel="#{cms['users.error.strongPassword']}"
      label="#{cms['users.add.confirmPassword']}"&gt;
      &lt;f:validateLength minimum="6" /&gt;
      &lt;f:validator validatorId="passwordValidator" /&gt;
      &lt;f:attribute name="password" value="#{password.value}" /&gt;
  &lt;/p:password&gt;
  &lt;p:message for="passwordConfirm" /&gt;
  
  &lt;h:outputLabel value="#{cms['users.add.active']}" /&gt;
  &lt;h:selectOneMenu value="#{editUserController.user.active}"&gt;
      &lt;f:selectItem itemValue="true" itemLabel="Sí" /&gt;
      &lt;f:selectItem itemValue="false" itemLabel="No" /&gt;
  &lt;/h:selectOneMenu&gt;
  &lt;h:outputText value="" /&gt;
  
  &lt;h:outputLabel value="#{cms['users.add.role']}" /&gt;
  &lt;h:selectOneMenu value="#{editUserController.securityRole}"&gt;
      &lt;f:selectItems value="#{editUserController.securityRoles}" /&gt;
  &lt;/h:selectOneMenu&gt;
  &lt;h:outputText value="" /&gt;
  
  &lt;h:outputText value="" /&gt;
  &lt;p:commandButton action="#{editUserController.saveUser}"
      style="margin-top:20px;" ajax="false"
      value="#{cms['general.save']}"&gt;&lt;/p:commandButton&gt;
  </code></pre>
  
  <p></p>
  
  <p>This is the Managed Bean (EditUserController.java):</p>
  
  <pre><code>public class EditUserController extends GeneralController implements
      Serializable {
  
  private static final long serialVersionUID = 1L;
  
  @ManagedProperty("#{param.id}")
  private Integer id;
  
  private UserService userService;
  
  private SecurityRoleService securityRoleService;
  
  private User user;
  
  private String confirmPassword;
  
  private Map&lt;String, SecurityRole&gt; securityRoles;
  
  private String securityRole;
  
  @PostConstruct
  public String checkUser() {
      try {
          FacesContext facesContext = FacesContext.getCurrentInstance();
          this.id = Integer.parseInt(facesContext.getExternalContext().getRequestParameterMap().get("id"));
  
          setUser(userService.findById(id));
          Map&lt;String, SecurityRole&gt; secRoles = new ListUtils&lt;String&gt;().toMap(
                  securityRoleService.findAll(0, 30, "", ""), "name");
  
          setSecurityRoles(secRoles);
  
          setSecurityRole(user.getSecurityRole().getId().toString());
  
          return "edit";
      } catch (EntityNotFoundException e) {
          return FATAL;
      } catch (NoSuchMethodException e) {
          return FATAL;
      } catch (InvocationTargetException e) {
          return FATAL;
      } catch (IllegalAccessException e) {
          return FATAL;
      }
  }
  
  public String saveUser() {
      try {   
          System.out.println("User saved!!");
          return SUCCESS;
      } catch (Exception e) {
          FacesContext.getCurrentInstance().addMessage(
                  null,
                  new FacesMessage(MessageProvider.getMessageProvider()
                          .getValue("cms", "general.error")));
          return ERROR;
      }
  }
  
  //getters and setters ...
  </code></pre>
  
  <p>With the @Postconstruct method I initialize the <em>user</em> variable according to the id <em>param</em>.
  Then the form is displayed correctly with all data prefilled. But when I click in the 
  commandButton I get an exception:</p>
  
  <pre><code>Error creating bean with name 'editUserController': Invocation of init method failed; nested exception is java.lang.NumberFormatException: null
  </code></pre>
  
  <p>I understand where is the error. In the @Postconstruct method the variable <em>id</em> has not 
  been "initialized" because there is not any <em>id</em> parameter in the request. I know I can solve this
  scoping the bean to session or application, but it does not make any sense to have the bean there
  just to perform this simple action. </p>
  
  <p>How can I do this simple form? I just want to get an id parameter, show a form with prefilled data
  then give the posibility to change the data an finally submit the form to save the new values
   into the DB</p>
  
  <p>Thank you all</p>