title: How to dynamically add a component using "Faces.includeCompositeComponent"?. Websphere 8 and ICEFaces 3
body: <p>I´m trying to dynamically add a composite component to another UIComponent using a small adaptation of OmniFaces <code>Faces.includeCompositeComponent(...)</code>. I´ve tried to recreate the behavior of <a href="https://stackoverflow.com/questions/15828540/programmatically-create-and-add-composite-component-in-backing-bean">this question</a> but it´s not working.</p>
  <p>The code for the adapted method is the following (I took it from <em>OmniFaces v1.6</em>)</p>
  <pre><code>/**
     * Create and include the composite component of the given library ane
     * resource name as child of the given UI component parent and return the
     * created composite component. This has the same effect as using
     * &lt;code&gt;&amp;lt;my:resourceName&amp;gt;&lt;/code&gt;. The given component ID must be unique
     * relative to the current naming container parent and is mandatory for
     * functioning of input components inside the composite, if any.
     * 
     * @param parent The parent component to include the composite component in.
     * @param libraryName The library name of the composite component.
     * @param resourceName The resource name of the composite component.
     * @param id The component ID of the composite component.
     * @return The created composite component, which can if necessary be further
     *         used to set custom attributes or value expressions on it.
     * @since 1.5
     */
    public static UIComponent includeCompositeComponent(UIComponent parent, String libraryName, String resourceName, String id)
    {
      // Prepare.
      FacesContext context = FacesContext.getCurrentInstance();
      Application application = context.getApplication();
      FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
  
      // This basically creates &lt;ui:component&gt; based on &lt;composite:interface&gt;.
      Resource resource = application.getResourceHandler().createResource(resourceName, libraryName);
      UIComponent composite = application.createComponent(context, resource);
      composite.setId(id); // Mandatory for the case composite is part of UIForm!
                           // Otherwise JSF can't find inputs.
  
      // This basically creates &lt;composite:implementation&gt;.
      UIComponent implementation = application.createComponent(UIPanel.COMPONENT_TYPE);
      implementation.setRendererType(&quot;javax.faces.Group&quot;);
      composite.getFacets().put(UIComponent.COMPOSITE_FACET_NAME, implementation);
  
      // Now include the composite component file in the given parent.
      parent.getChildren().add(composite);
      parent.pushComponentToEL(context, composite); // This makes #{cc} available.
      try
      {
        faceletContext.includeFacelet(implementation, resource.getURL());
      }
      catch (IOException e)
      {
        throw new FacesException(e);
      }
      finally
      {
        parent.popComponentFromEL(context);
      }
  
      return composite;
   }
  </code></pre>
  <p>And this is the way I´m trying to use it</p>
  <pre><code>JSFUtils.includeCompositeComponent(panelGroup, &quot;comp&quot;, 
     &quot;../inc-templates/test.xhtml&quot;, JSFUtils.createUniqueId());
  </code></pre>
  <p>Also, I created the &quot;test.xhtml&quot; file and placed it under <strong>WEB-INF/inc-templates/test.xhtml</strong> . Here is its content:</p>
  <p><strong>test.xhtml</strong></p>
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt;
  &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
  &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;
        xmlns:cc=&quot;http://java.sun.com/jsf/composite&quot;
        xmlns:ui=&quot;http://java.sun.com/jsf/facelets&quot;
        xmlns:h=&quot;http://java.sun.com/jsf/html&quot;
        xmlns:f=&quot;http://java.sun.com/jsf/core&quot;
        xmlns:p=&quot;http://primefaces.org/ui&quot;
        xmlns:composite=&quot;http://java.sun.com/jsf/composite&quot;&gt;
      &lt;!-- INTERFACE --&gt;
      &lt;cc:interface&gt;
  
      &lt;/cc:interface&gt;
  
      &lt;!-- IMPLEMENTATION --&gt;
      &lt;cc:implementation&gt;
          &lt;h:outputText value=&quot;TEST&quot;/&gt;
      &lt;/cc:implementation&gt;
  &lt;/html&gt;
  </code></pre>
  <p>Last but not least, I´ve added the test.xhtml tag to the <strong>taglib.xml</strong>, like this:</p>
  <pre><code>&lt;tag&gt;
    &lt;tag-name&gt;test&lt;/tag-name&gt;
    &lt;source&gt;../inc-templates/test.xhtml&lt;/source&gt;
  &lt;/tag&gt;
  </code></pre>
  <p>The thing is, when I run the code and try to insert the component I get the following exception</p>
  <pre><code>Caused by: java.lang.NullPointerException: Argumentfehler: Parameter componentResource ist null
          at com.sun.faces.util.Util.notNull(Util.java:314)
          at com.sun.faces.application.ApplicationImpl.createComponent(ApplicationImpl.java:928)
          at org.my.JSFUtils.includeCompositeComponent(JSFUtils.java:496)
  </code></pre>
  <p>and the line <strong>at org.my.JSFUtils.includeCompositeComponent(JSFUtils.java:496)</strong> refers exactly to the line of code <code>UIComponent composite = application.createComponent(context, resource);</code>, which in fact reveals that the resource is not being created in the previous line: <code>Resource resource = application.getResourceHandler().createResource(resourceName, libraryName);</code></p>
  <p>Does anyone have any idea why could this be happening?. I´ve even tried changing the location of the resource to <strong>inc-templates/test.xhtml</strong> and even just <strong>test.xhtml</strong> when invoking the method, but nothing works... I always get the same error.</p>
  <p>By the way, I´m trying to deploy in Websphere 8 and I´m using ICEFaces 3.</p>
  <p>Thanks in advance!</p>