NullPointerException when trying to save my Form

2019-09-10 22:30发布

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)

My form:

@(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"/>
  }
}

My model:

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

My controller:

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

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!

EDIT

enter image description here enter image description here

2条回答
小情绪 Triste *
2楼-- · 2019-09-10 23:23

Does this help ?

Issue with bindFromRequest in Play! Framework 2.3

I have been struggling with exactly the same problem: bindFromRequest returned nulls for "name" field. I did exactly the same what a guy in this Play for Java introduction video did: youtube.com/watch?v=bLrmnjPQsZc . But still no luck. I've been working on Windows 7 with JDK 1.8. IDE: Eclipse 4.4.0. And I run activator through cygwin.

This is what solved the problem for me:

In Eclipse: Project -> Build Automatically - > turn off In cygwin: ./activator clean; ./activator compile; ./activator run; After this, bindFromRequest binds name correctly and puts it into the database.

查看更多
该账号已被封号
3楼-- · 2019-09-10 23:34

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.

查看更多
登录 后发表回答