尝试保存表单

时间:2016-08-14 07:37:50

标签: java playframework playframework-2.0

我是PlayFramework的新手,我正在关注Udemy的视频课程。但是,Play的版本是2.3.9,我试图用2.5.4版本来做;

当我尝试保存表单时,它会出现以下错误:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[CompletionException: java.lang.NullPointerException]]
    at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:280)
    at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:206)
    at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
    at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
    at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:98)
    at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
    at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
    at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344)
    at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
Caused by: java.util.concurrent.CompletionException: java.lang.NullPointerException
    at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:292)
    at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:308)
    at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:593)
    at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577)
    at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474)
    at java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:1977)
    at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:21)
    at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:18)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
    at scala.concurrent.BatchingExecutor$Batch$$anonfun$run$1.processBatch$1(BatchingExecutor.scala:63)
Caused by: java.lang.NullPointerException: null
    at com.avaje.ebean.Model.save(Model.java:208)
    at controllers.Services.save(Services.java:28)
    at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$6$$anonfun$apply$6.apply(Routes.scala:227)
    at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$6$$anonfun$apply$6.apply(Routes.scala:227)
    at play.core.routing.HandlerInvokerFactory$$anon$4.resultCall(HandlerInvoker.scala:157)
    at play.core.routing.HandlerInvokerFactory$$anon$4.resultCall(HandlerInvoker.scala:156)
    at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$14$$anon$3$$anon$1.invocation(HandlerInvoker.scala:136)
    at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:73)
    at play.http.HttpRequestHandler$1.call(HttpRequestHandler.java:54)
    at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:108)

我的表格:

@(serviceForm : Form[Service])
@import helper._


@main("Service info"){
  <h1>Service Information</h1>
  @helper.form(action = routes.Services.save()){
    <fieldset>
      <legend>Service</legend>
      @helper.inputText(serviceForm.field("code"), '_label -> "Code")
      @helper.inputText(serviceForm.field("description"), '_label -> "Description")
    </fieldset>
    <input type="submit" value="Save"/>
  }
}

我的模特:

@Entity
public class Service extends Model {
    @Id
    public String code;
    public String description;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

我的控制器:

public class Services extends Controller {

    @Inject FormFactory formFactory;

    public Result list(){
        return TODO;
    }

    public Result addService(){
        Form<Service> serviceForm = formFactory.form(Service.class);
        return ok(info.render(serviceForm));
    }

    public Result save(){
        Form<Service> serviceForm = formFactory.form(Service.class);
        Service service = serviceForm.bindFromRequest().get();
        service.save();

        return  redirect(routes.Services.addService());
    }
}

我真的不知道从哪里开始。谷歌谈论这个问题的帖子并不多。任何帮助都会很棒!

修改

enter image description here enter image description here

2 个答案:

答案 0 :(得分:0)

这有帮助吗?

Issue with bindFromRequest in Play! Framework 2.3

  

我一直在努力解决完全相同的问题:bindFromRequest   返回“name”字段的空值。我的确和一个男人完全一样   这个Play for Java介绍视频做了:   youtube.com/watch?v=bLrmnjPQsZc。但仍然没有运气。我一直在工作   在Windows 7上使用JDK 1.8。 IDE:Eclipse 4.4.0。我运行激活器   通过cygwin。

     

这就解决了我的问题:

     

在Eclipse中:项目 - &gt;自动构建 - &gt;关闭在cygwin中:   ./activator clean; ./activator编译; ./activator运行;在这之后,   bindFromRequest正确绑定名称并将其放入数据库。

答案 1 :(得分:0)

对于那些可能有相同或相关问题的人。 Please check my new post here。基本上,我的问题是Ebean插件版本。

此外,您可以在我的存储库here上看到我的更改。