accessing PrimeFaces command button from bean to add action listener
<p>I have the following command button in the view with ID "save":</p>

<pre><code>        &lt;p:panel style="border:none;text-align:left;margin:0;"&gt;

            &lt;p:commandButton value="Save Document" id="save" icon="fa fa-save"
                disabled="#{dIGRCController.digrc.qconce == '020'}"&gt;

                &lt;f:param name="validate" value="true" /&gt;

            &lt;/p:commandButton&gt;

            &lt;p:commandButton value="Clear" icon="fa fa-undo"&gt;&lt;/p:commandButton&gt;

        &lt;/p:panel&gt;
</code></pre>

<p>I am trying to dynamically assign a different actionListener. If the user wants to INSERT some new record, I want it to call the insert method. If the user wants to update an existing record, it should call the update method. </p>

<p>Right now I am trying to do this:</p>

<pre><code>@PostConstruct
public void init() {

    // setting the action listener of the Save Document button
    UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();

    // UIComponent button = viewRoot.findComponent("save");

    CommandButton button = (CommandButton) viewRoot.findComponent("save");

    FacesContext context = FacesContext.getCurrentInstance();

    MethodExpression methodExpression = context
            .getApplication()
            .getExpressionFactory()
            .createMethodExpression(context.getELContext(),
                    "#{dIGRCController.updateDocument}", null,
                    new Class[] { DIGRCController.class });

    button.addActionListener(new MethodExpressionActionListener(
            methodExpression));

}
</code></pre>

<p>I am getting a null pointer exception on the line:</p>

<pre><code>button.addActionListener(new MethodExpressionActionListener(
        methodExpression));
</code></pre>

<p>What am I doing wrong? Is there another way to accomplish what I am trying to do? I am using JSF 2.2, PrimeFaces 5.3 and OmniFaces 1.11.</p>