1
votes

I'm trying to bind a form to a request but it fails with the following error:

Execution exception
[RuntimeException: Cannot instantiate class controllers.Application$RequestData. It must have a default constructor]

The error description seems straight forward and simple, but looking at the code:

public class RequestData {
    @Required
    public String id;

    public RequestData() { }

    public RequestData(String id) {
        this.id = id;
    }
}

public static Result index() {
    ...
    Form<RequestData> requestDataForm = form(RequestData.class);
    RequestData requestData = requestDataForm.bindFromRequest().get();
    ...
}

You can see that the class indeed has a default constructor and so this error is not clear at all.

Any ideas? Thanks.


Edit

What's amusing is that in the official documentation, the example they use do not have a default constructor.

1
When the compiler sees an empty constructor, does this get 'optimised away' - just a guess? Remember, that's just your source, not bytecode. - Michael
Um, why would that happen? And more importantly, how do I find out? - Nitzan Tomer

1 Answers

5
votes

From the error message it looks like your RequestData class is an inner class of the Application class, and in this case an instance of Application is required to create an instance of RequestData (because RequestData is not static). If I have this right, then either make RequestData its own class or -- if you want to keep it as an inner class -- make it a static inner class.