title: Accessing dynamic UIComponents in JSF Managed Bean
body: <p>I have a JSF page in which I am iterating a list within <code>&lt;h:dataTable&gt;</code> to display some rows containing checkbox, some text and a textbox.</p>
  
  <p>I have to validate the <code>&lt;h:dataTable&gt;</code> so that when a user checks the checkbox, he has to enter some text inside the textbox.</p>
  
  <p>This is my JSF page.</p>
  
  <pre><code> &lt;h:form prependId="false" id="form"&gt;
      &lt;h:dataTable id="rm" width="100%" cellspacing="4"
      value="#{controller.alertTriggers}" var="alt"
          columnClasses="c1,c2,c3,c4"&gt;                    
  
  
              &lt;h:column&gt;
                &lt;h:selectBooleanCheckbox value="#{alt.checkValue}" id="checkbox"/&gt;
             &lt;/h:column&gt;
              &lt;h:column&gt;
                 &lt;h:outputText value="#{alt.id}" /&gt;                                       
              &lt;/h:column&gt;
              &lt;h:column&gt;
                &lt;h:outputFormat value="#{alt.msg1}" /&gt;                              
              &lt;/h:column&gt;
              &lt;h:column&gt;                                              
                   &lt;h:message for="emailID" id="email" styleClass="validation-error"/&gt;
                   &lt;h:inputText value="#{alt.mailId}" id="emailID" style="width: 87%;" /&gt;
  
              &lt;/h:column&gt;                                         
  
      &lt;/h:dataTable&gt;                                          
  &lt;/h:form&gt;                                       
  </code></pre>
  
  <p>I have given the id of all the checkboxes as <code>checkbox</code> and id of all textboxes as <code>emailID</code>. When the page is rendered, on checking the page source, I found that the ids of the checkboxes are 'rm:0:checkbox','rm:1:checkbox'... and those of the textboxes are 'rm:0:emailID','rm:1:emailID'..</p>
  
  <p>In the controller, I want to access these dynamic text boxes and check boxes for which I use the following code:</p>
  
  <pre><code> FacesContext context = FacesContext. getCurrentInstance();
  
  
                    for (int i=0;i&lt;9;i++){
                         UIInput u=(UIInput) FacesContext.getCurrentInstance().getViewRoot().findComponent( "form:rm:" +i+":checkbox" );
                          if ((Boolean) u.getValue()){
                                UIInput ui=(UIInput) FacesContext.getCurrentInstance().getViewRoot().findComponent( "form:rm:" +i+":emailID" );
  
                                 //code
  
                         }
                   }
  </code></pre>
  
  <p>But this is giving java.lang.NullPointerException</p>
  
  <p>Even using the code:</p>
  
  <pre><code>  UIInput u=(UIInput) FacesContext.getCurrentInstance().getViewRoot().
     findComponent( "form:rm:0:checkbox" ); gives the same exception.
  </code></pre>
  
  <p>But if I use</p>
  
  <pre><code>     UIInput u=(UIInput) FacesContext.getCurrentInstance().getViewRoot().
         findComponent( "form:rm:checkbox" );
  </code></pre>
  
  <p>it doesn't give a Null Pointer Exception but I don't know whether which checkbox's value is it giving.</p>
  
  <p>So, in summary,</p>
  
  <p><strong>JSF generates the ids as rm:1:checkbox,rm:2:checkbox etc., but when I try to access this UI component in JSF page, I am not able to do it.</strong> </p>
  
  <p>Am I missing something ? </p>