Sharon Bond
Sharon Bond

Reputation: 55

MethodExpression IllegalArgumentException: wrong number of arguments

I've got a JSF page that is rendered dynamically - all components are created by the backing bean and added to a grid. I have a command button that I've created a MethodExpression to set the method to call when the button is clicked. But clicking the button results in the exceptions "java.lang.IllegalArgumentException: wrong number of arguments" and "javax.faces.FacesException: wrong number of arguments".

The code that creates the button is

HtmlCommandButton addBtn1 = (HtmlCommandButton) FacesContext.getCurrentInstance().
getApplication().createComponent(HtmlCommandButton.COMPONENT_TYPE);
addBtn1.setId("addBtn1");
addBtn1.setValue("Add Row");
String addBtn1Action = "#{dynamicComponentTest.addBtn1_action}";
Class[] args = new Class[]{ActionEvent.class};
MethodExpression addBtn1ME = FacesContext.getCurrentInstance().getApplication().
getExpressionFactory().createMethodExpression(
FacesContext.getCurrentInstance().getELContext(), addBtn1Action, null, args);
addBtn1.setActionExpression(addBtn1ME);
mainGrid.getChildren().add(addBtn1);

Then the action method's signature is

public String addBtn1_action(ActionEvent event)

When I run my test app and click the button, I get the exceptions. However, if I change the signature to

public String addBtn1_action(ActionEvent[] event)

I get a MethodNotFoundException where it can't find "[email protected]_action(javax.faces.event.ActionEvent)"

Has anyone succesfully solved this issue? I have tried both null and String.class as the type of the return value in the createMethodExpression and that doesn't help solve it.

Upvotes: 1

Views: 1366

Answers (1)

partlov
partlov

Reputation: 14277

Action method should not have parameters. ActionEvent parameter is for actionListeners. So, remove that parameter, and change createMethodExpression call according to that. Also, return type of your action method is String. Third parameter of createMethodExpression is return type. You should put String.class in place of null. Finally:

FacesContext.getCurrentInstance().getApplication().getExpressionFactory().
        createMethodExpression(
        FacesContext.getCurrentInstance().getELContext(),
          addBtn1Action, String.class, new Class[0]);

And action method signature:

public String addBtn1_action() {
    // ...
}

Upvotes: 1

Related Questions