尝试保存我的表单时出现NullPointerException [英] NullPointerException when trying to save my Form

查看:133
本文介绍了尝试保存我的表单时出现NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PlayFramework的新手,并且正在观看有关Udemy的视频课程.但是,它们在Play中的版本为2.3.9,而我正在尝试使用2.5.4版本.

I'm new to PlayFramework and I'm following a video course on Udemy. However, their version from Play is 2.3.9, and I'm trying to do it with the version 2.5.4;

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

When I try to save my form, it gives me the following error:

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

我真的不知道从这里去哪里.而且,在Google上没有多少关于这一点的文章.任何帮助都会很棒!

I really don't know where to go from here. And there's not many posts on Google talking about this. Any help would be great!

编辑

推荐答案

针对可能存在相同或相关问题的人员. 请在此处查看我的新帖子.基本上,我的问题是Ebean插件版本.

For those who might have the same, or related, problem. Please check my new post here. Basically, my problem was with Ebean plugin version.

此外,您还可以在我的存储库中看到我的更改这里

Also, you can see my changes on my repository here.

这篇关于尝试保存我的表单时出现NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆