title: JSF Ajax addClientBehavior gives null.pointer.exception
body: <p>I'm trying to add programatically an Ajax client behavior to a custom component but when I do I get a null.pointer.exception (as an Alert in the browser no error in the logs), if I add the behavior in the .xhtml it works fine but I really need to add them programatically (actually the component is going to be rendered dinamically from a top component (dashboard -> N columns -> N widgets per column all from a Database)</p>
  
  <p>I have tested this creating a simple DataList component</p>
  
  <p>Here is the relevant code...</p>
  
  <p>DataListRenderer</p>
  
  <pre><code>@Override
      public void encodeEnd(FacesContext context, UIComponent component) throws IOException{
          DataList datalist = (DataList)component;
          ResponseWriter writer = context.getResponseWriter();
  
          .. bunch of rendering options....
  
          // If sortable then we add the sortable script.
          // This basically ends up generating a call to jsf.ajax.request(source,event,params);
          if (sortable != null &amp;&amp; sortable.equalsIgnoreCase("true")) {
              ScriptUtils.startScript(writer, clientId);
              writer.write("$(function() {");
  
              writer.write("$(EasyFaces.escapeId('" + clientId + "')).sortable({");
              writer.write("update: function (event, ui) { ");
  
              writer.write(new AjaxRequest().addEvent(StringUtils.addSingleQuotes("update"))
                                  .addExecute(StringUtils.addSingleQuotes(datalist.getClientId()))
                        .addSource(StringUtils.addSingleQuotes(datalist.getClientId()))
                        .addOther("sourceId", "ui.item.attr('id')")
                        .addOther("dataValue", "ui.item.attr('data-value')")
                        .addOther("dropedPosition", "ui.item.index()")
                        .getAjaxCall());
  
              writer.write(" } ");
              encodeClientBehaviors(context, datalist);
              writer.write("});");
              writer.write("});");
              ScriptUtils.endScript(writer);
  
          }
  
           // This is where I add the behavior
          AjaxBehavior ajaxBehavior = (AjaxBehavior) FacesContext.getCurrentInstance().getApplication().createBehavior(AjaxBehavior.BEHAVIOR_ID);
          ajaxBehavior.setRender(Arrays.asList("@form"));
          ajaxBehavior.setExecute(Arrays.asList("@form"));
          MethodExpression listener = FacesContext.getCurrentInstance().getApplication()
                  .getExpressionFactory().createMethodExpression(context.getELContext(),"#{dataListManager.updateModel}", null,
                          new Class[] { AjaxBehaviorEvent.class });
          ajaxBehavior.addAjaxBehaviorListener(new AjaxBehaviorListenerImpl(listener));
          datalist.addClientBehavior(datalist.getDefaultEventName(), ajaxBehavior);
  
      }
  
      ... rest of the code
  </code></pre>
  
  <p>Now the .xhtml is as follows:</p>
  
  <pre><code>...
  
      h:form&gt;
        &lt;et:dataList id="dl" value="#{easyfacesBean.data}" itemValue="foo" var="xx" sortable="true"&gt;
          &lt;h:outputText id="txt" value="#{xx}" /&gt;
        &lt;/et:dataList&gt;
      &lt;/h:form&gt;
  ...
  </code></pre>
  
  <p>This renders the code Ok, the list actually can be ordered but when re-ordered it gives me the null.pointer.exception error</p>
  
  <p>This is what comes back from the server:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
  &lt;partial-response&gt;&lt;error&gt;&lt;error-name&gt;class java.lang.NullPointerException&lt;/error-name&gt;&lt;error-message&gt;&lt;![CDATA[]]&gt;&lt;/error-message&gt;&lt;/error&gt;&lt;/partial-response&gt;
  </code></pre>
  
  <p>Now, If I comment this line</p>
  
  <pre><code>datalist.addClientBehavior(datalist.getDefaultEventName(), ajaxBehavior);
  </code></pre>
  
  <p>And simply add the  tag like so:</p>
  
  <pre><code>&lt;h:form&gt;
        &lt;et:dataList id="dl" value="#{easyfacesBean.data}" itemValue="foo" var="xx" sortable="true"&gt;
        &lt;f:ajax /&gt;
          &lt;h:outputText id="txt" value="#{xx}" /&gt;
        &lt;/et:dataList&gt;
      &lt;/h:form&gt;
  </code></pre>
  
  <p>Everything works fine.. any ideas? I would settle for a way to actually know where the null.point.exception is ..</p>
  
  <p>BTW if I don't comment the line that actually add's the behavior and add the &lt; f:ajax > tag the error changes to java.lang.IndexOutOfBoundsException</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
  &lt;partial-response&gt;&lt;error&gt;&lt;error-name&gt;class java.lang.IndexOutOfBoundsException&lt;/error-name&gt;&lt;error-message&gt;&lt;![CDATA[Index: 1, Size: 1]]&gt;&lt;/error-message&gt;
  </code></pre>
  
  <p>Regards!</p>