Application Exception in tapestry subforms - Parameter is bound to null

1.9k views Asked by At

I want to create a subform with tapestry5:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd">

    <t:TextField t:id="name" />
</html>

and use it like this:

<form t:type="form" t:id="testForm">
    <t:testComponent name="name" />
    <input type="submit"/>
</form>

TestComponent.java:

import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;

public class TestComponent {

    @Parameter(required = true, allowNull = false)
    @Property
    private String name;
}

so that i can use the value of 'name' like:

@Property
private String name;

void onSuccessFromTestForm() {
    System.out.println(name);
}

But all i get is an application exception:

Render queue error in BeginRender[Index:testcomponent.name]: Failure reading parameter 'value' of component Index:testcomponent.name: Parameter 'name' of component Index:testcomponent is bound to null. This parameter is not allowed to be null.

Whats the problem?

2

There are 2 answers

2
joostschouten On

Tapestry is telling you that the component containing your Form and your TestComponent has a property "name" with value null. So you problem is not in your TestComponent, but one component/page higher. Assign a value to name and you should be good.

Edit

If you mean to allow people to assign a value though your form and allow null values while rendering the page, remove the allowNull = false from your @Parameter in your TestComponent. I'm assuming you want to force the user to provide a value for the name field before submitting the form. This is done on the input field by adding the t:validate="required" attribute, not on the @Parameter. The @Parameter tells tapestry how the instance variable interacts with its container, it sais nothing about how the variable is used within its own component.

0
Michal Gruca On

@Parameter annotation is like a parameter to a constructor. Basically, your code is saying, something like

public TestComponent(String name){
  if(name == null) thrown new Exception("No Nulls in here boy");
}

It's good that you do that, it prevents NullPointers in case that you want to do some processing in the component, and searching for root of NPE is most annoying thing in the world. What's more, you got required set to true, which means that you must pass some value to the component. You have to initialize the name variable to something, empty string is good enough, as it won't mess up with the behavior you want to achieve, but will satisfy tapestry.

That would fix the current technical issue you've got. As for your real issue, with the validation, like joostschouten mentioned, you need to set validation rules. There are several ways how you can achieve that, please either add t:validate like joostschouten mentioned or add onValidateFromName(String name) throws ValidationException{...} in the component. Second method is bit of an overkill for such simple validation, but may be necessary for anything more complicated

Some references on the topic:

http://tapestry.apache.org/forms-and-validation.html http://tapestry.apache.org/component-parameters.html http://jumpstart.doublenegative.com.au/jumpstart/examples/input/validators http://jumpstart.doublenegative.com.au/jumpstart/examples/input/morevalidation