预期为'}',但发现eof

时间:2018-12-07 02:38:15

标签: java playframework playframework-2.0

我正在使用播放框架2.5.10 编译错误为}' expected but eof found.。 我无法匹配一些未命中的代码。在哪里调试?

这是我的Book.java模型

package models;

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;
    }

    private static Set<Book> books;

    static {
        books = new HashSet<>();
        books.add(new Book(id: 1, title: "C++", price : 10, author: "ABC");
        books.add(new Book(id: 2, title: "Java", price : 20, author: "DEF");
    }

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

    public static Book findById(Integer id) {
        for (Book book : books) {
            if (id.equals(book.id)) {
                return book;
            }
        }

        return null;
    }

    public static void add (Book book){
        books.add(book);
    }

    public static boolean remove(Book book){
        return books.remove(book);
    }
}

这是编译错误

[info] Compiling 1 Scala source and 3 Java sources to /Users/Play/PROJ_NAME/target/scala-2.11/classes...
[error] /Users/Play/PROJ_NAME/app/models/Book.java:46: `}' expected but eof found.
[error] }
[error]  ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[info] Compiling 1 Scala source and 3 Java sources to /Users/Play/PROJ_NAME/target/scala-2.11/classes...
[error] /Users/Play/PROJ_NAME/app/models/Book.java:46: `}' expected but eof found.
[error] }
[error]  ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] application - 

! @7a6fl9c3b - Internal server error, for (GET) [/books] ->

play.sbt.PlayExceptions$CompilationException: Compilation error[`}' expected but eof found.]
        at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27)
        at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27)
        at scala.Option.map(Option.scala:145)
        at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:49)
        at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:44)
        at scala.Option.map(Option.scala:145)
        at play.sbt.run.PlayReload$.taskFailureHandler(PlayReload.scala:44)
        at play.sbt.run.PlayReload$.compileFailure(PlayReload.scala:40)
        at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17)
        at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17)

谢谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您的static屏蔽已关闭,并且您缺少任何import。 Java没有命名的字典参数语法,应该看起来像

private static Set<Book> books; // do not use raw-types

static {
    books = new HashSet<>(); // diamond operator
    books.add(new Book(1, "C++", 10, "ABC")); // need to close the .add() call
    books.add(new Book(2, "Java", 20, "DEF"));
}

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

并确保您

import java.util.HashSet;
import java.util.Set;