5

I am new to Play Framework, and attempting to build a Todo-list from this manual.

When I try to run the application I get the error:

Compilation Error
error: method render in class index cannot be applied to given types;

My code is (the relevant parts):

MainController.java:

final static Form<Task> taskForm = Form.form(Task.class);

public static Result tasks() {
    return ok(
            views.html.index.render(Task.all(), taskForm)
    );
}

index.sacala.html:

@(tasks: List[Models.Task], taskForm: Form[Models.Task])

I looked around, the closest thread I found was this one, but I was not able to resolve the issue using it (might be due to lack of understanding of the environment / framework...).

One last thing that is worth mentioning:
If I change 'index.sacala.html' to be parameter-less (and change the 'MainController' accordingly, everything works perfectly.

Would appreciate any thoughts about resolving this compilation error.

EDIT:
The Task.all() code is:

public static List<Task> all() {
    return new ArrayList<Task>();
}
Community
  • 1
  • 1
Avi Turner
  • 9,342
  • 7
  • 43
  • 68

3 Answers3

4

Most probably your package is models NOT Models isn't it?

BTW this package is autoimported so you can just use:

 @(tasks: List[Task], taskForm: Form[Task])

Hm, changes... Actually log in the console says everything

[error] /www/play20apps/testing/Todo-List/app/controllers/MainController.java:24: error: method render in class index cannot be applied to given types;
[error]         return ok(views.html.index.render(Task.all(), taskForm));
[error]                                   ^
[error]   required: List<Task>,play.api.data.Form<Task>
[error]   found: List<Task>,play.data.Form<Task>
[error]   reason: actual argument play.data.Form<Task> cannot be converted to play.api.data.Form<Task> by method invocation conversion
[error] 1 error

especially these lines:

[error]   required: List<Task>,play.api.data.Form<Task>
[error]   found: List<Task>,play.data.Form<Task>

TBH I didn't ever test the Activator but it looks that imports play.api.data.Form into views, which is incorrect for Java controllers. solution is full qualified path for Form:

@(tasks: java.util.List[Task], taskForm: play.data.Form[Task])

As mentioned in comment *.api.* imports are for Scala and normal are for Java, that's the rule of the thumb in Play 2.+

PostScriptum: Just realized that in your build.sbt you have play.Project.playScalaSettings and actually it should be play.Project.playJavaSettings, this change fixes your problems with Activator.

biesior
  • 54,554
  • 10
  • 118
  • 177
  • I was hopping to get an answer from you :0) I have noticed that you had the best quality answers in this subject... Actually it was `Models` I changed it to `models` still no luck. It is probably worth mentioning that I used [TypeSafe Activator](http://typesafe.com/platform/runtime/playframework) for this project. I did not have the `models` folder by default, I have manually created it under `controllers` and if I do not add the import, it not being imported (although the `app` folder, which is the parent of `controller` is marked as `source` in the `project structure` window) – Avi Turner Feb 24 '14 at 17:03
  • Are you sure the imports in your controller are correct? You import play.*, play.mvc.*, etc but I think you are supposed to import play.api.mvc.* and co. Not sure if this apply to using Play with Java but that's how it is in Scala, you only import things under play.api – vptheron Feb 24 '14 at 18:52
  • @vptheron NO! `*.api.*` imports are for Scala and normal are for Java, that's the rule of the thumb in Play 2.+ – biesior Feb 24 '14 at 19:09
  • Just a note, I have implemented everything from scratch without the activator and it works perfectly. Not sure what the difference is, On my side I have done everything the same... – Avi Turner Feb 24 '14 at 19:13
  • Thanks, seems that activator is still a bit buggy. They do a nice job but maybe I should have waited until the product is a bit more mature. – Avi Turner Feb 25 '14 at 07:02
  • Have no experience with Activator, rather saw it several times just cause was curious. – biesior Feb 25 '14 at 08:06
  • 1
    Be aware for play 2.3 there is not playJavaSettings. See the migration guide https://playframework.com/documentation/2.3.x/Migration23 – Jorge Nunez Newton Feb 04 '15 at 14:19
2

It looks like Task.all() returns a Java list, while the scala template is probably expecting a Scala list.

I woul suggest changing the return type of Task.all() if possible, or fully qualified the definition in the template:

@(tasks: java.util.List[Models.Task], taskForm: Form[Models.Task])
vptheron
  • 7,218
  • 23
  • 33
  • changed the definition in the template as you have suggested, same result... Probably a newbie question but - To what do you suggest changing the `Task.all()` return type to? – Avi Turner Feb 24 '14 at 18:00
1

Although biesior's answer has some nice insights and tips, it did not solve the issue.
At the end I have abndoned type TypeSafe Activator and created the site from scratch using play comamnd line and it worked perfectly.

I never found the origin of the issue in the question, I am leaving this answer for a future reference for the googlers.

If any one has a better solution, please leave your answer and if it works I will mark it as accepted.


EDIT:

@biesior was kind enough to go through my code and he did find the issue. If you have the same issue, take a look at his answer.

Avi Turner
  • 9,342
  • 7
  • 43
  • 68