Pierto
Pierto

Reputation: 31

How to fix "ambiguous method call" in my code in Java in play framework

I'm following the tutorial for play framework in Java (#13) on youtube and I stuck in Index Method of BookStore Application. I cant go any further since I get: "Ambiguous method call. Both render(Set,) in index and render(Set) in index$ match".

I tried to change Set for List but the massage I got was basically the same only in relation to List.

public class BooksController extends Controller {

    public Result index(){
        Set<Book> books = Book.allBooks();

        return ok(index.render(books));  //<--------- the error
    }        
}

public class Book {

    public Integer id;
    public String title;
    public Integer price;
    public String author;

    public Book(Integer id, String title,Integer price, String author){
        this.id = id;
        this.title = title;
        this.price = price;
        this.author = author;
    }

    public static Set<Book> books;

    static {
        books = new HashSet<>();
        books.add(new Book(1, "Java", 20, "ABC"));
        books.add(new Book(2,"C++", 30, "XYZ"));
    }

    public  static Set<Book> allBooks(){
        return books;
    } 
}

Upvotes: 3

Views: 874

Answers (1)

Yevgen
Yevgen

Reputation: 4709

You get this error because index from views.html.index.render and your method index() in BooksController have same name and compiler is confused what method to use. Just change name of your render method to something else for example booksIndex() and your problem will be gone.

public class BooksController extends Controller {

    public Result booksIndex(){
        Set<Book> books = Book.allBooks();

        return ok(index.render(books));
    }        
}

P.S. don't forget to change your routes file after

Upvotes: 2

Related Questions