在Form中播放2.5 java.lang.NullPointerException

时间:2016-04-20 06:24:28

标签: playframework form-submit playframework-2.5

我有这个小应用程序,我只想创建一个简单的表单并存储用户输入,但每次我尝试运行应用程序时都会得到 java.lang.NullPointerException ,它告诉我,当在definitionForm

中声明表单时,错误在控制器类的索引方法中

这是我的控制器

public class HomeController extends Controller {
@Inject 
public static FormFactory formFactory;

public Result index() {
    Form<Definition> definitionForm = formFactory.form(Definition.class);
    return ok(index.render(definitionForm,"Your new application is ready."));
}
public Result submit(){
    Form<Definition> definitionForm = formFactory.form(Definition.class).bindFromRequest();
    Definition definition=definitionForm.get();
    definition.save();
    return redirect(routes.HomeController.index());

}}

模型

@Entity
public class Definition extends Model {
@Id
public Long id;

@Constraints.Required
public String name;

public String definition;
public String category;
public static final Model.Find<Long,Definition> find = new Model.Find<Long,Definition>(){};
  }

这是错误

    ! @6pnmighcp - Internal server error, for (GET) [/] ->

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 controllers.HomeController.index(HomeController.java:27)
    at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$1$$anonfun$apply$1.apply(Routes.scala:157)
    at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$1$$anonfun$apply$1.apply(Routes.scala:157)
    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)
    at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:108)

我有

ebean.default = ["models.*"]

在我的应用程序conf中启用了Play Ebean插件,我不确定是什么问题。

1 个答案:

答案 0 :(得分:1)

只是把这个作为答案,以防有人偶然发现(Tijkijiki已经在评论中说明了这一点): FormFactory字段不应该是静态的

更多信息:

Play 2.5.x文档说明了这一点:

  

要包装一个类,你必须将一个play.data.FormFactory注入到Controller中,然后允许你创建表单:

     

表单userForm = formFactory.form(User.class);

因此将FormFactory注入控制器的正确方法如下:

package controllers;

import play.*;
import play.mvc.*;

public class Application extends Controller {

    @Inject FormFactory formFactory;

    ...
}