Play 2.4 Finder 抛出空指针异常
Posted
技术标签:
【中文标题】Play 2.4 Finder 抛出空指针异常【英文标题】:Play 2.4 Finder throws Null Pointer Exception 【发布时间】:2015-11-26 14:06:14 【问题描述】:我目前在 Play 2.4 中遇到了一个 Finder 实例的问题,它在使用时会返回一个空指针异常。这是尝试使用它的代码
public ExpressionList<ClientAuthorization> getAuthorizationQuery(String appId)
return ClientAuthorization.find.where().eq("app_id", appId).eq("active", 1);
还有模型类
package models;
import com.avaje.ebean.Model;
import javax.persistence.*;
import java.sql.Timestamp;
@Entity
@Table(name="client_authorization")
public class ClientAuthorization extends Model
public static final Finder<Long, ClientAuthorization> find = new Finder<>(ClientAuthorization.class);
@Id
public long clientAuthorizationId;
@Column(nullable=false, length=45)
public String appId;
@Column(nullable=false, length=45)
public String apiKey;
@Column(nullable=false)
public boolean active;
@Column(nullable = false)
public boolean isAdmin;
@Column(nullable=false)
public Timestamp createdDate;
@Column(nullable=false)
public Timestamp updatedDate;
@PrePersist
protected void createdAt()
this.createdDate = new Timestamp(System.currentTimeMillis());
this.updatedDate = new Timestamp(System.currentTimeMillis());
@PostPersist
protected void updatedAt()
this.updatedDate = new Timestamp(System.currentTimeMillis());
这适用于我在 Play 中创建的每个模型,所以我不确定发生了什么。这是我得到的
我非常感谢我能得到的任何帮助。这让我在我正在进行的升级项目中发疯。我做的最后一次升级很顺利,但是这次升级给我带来了问题。
附加信息:在 ClientAuthorization 静态调用 find 之后调用 where() 失败
更新
自从它出现在评论中以来,这不仅仅是 NPE 是什么的问题,我已经知道了。由于某种原因,finder 未初始化。然而,看看这个模型,以及我写过的每一个模型,都没有理由发生这种情况。
这是堆栈跟踪
@6o9455j27 - Internal server error, for (GET) [/] ->
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[NullPointerException: null]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:265) ~[play_2.11-2.4.4.jar:2.4.4]
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:191) ~[play_2.11-2.4.4.jar:2.4.4]
at play.api.GlobalSettings$class.onError(GlobalSettings.scala:179) [play_2.11-2.4.4.jar:2.4.4]
at play.api.DefaultGlobal$.onError(GlobalSettings.scala:212) [play_2.11-2.4.4.jar:2.4.4]
at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:94) [play_2.11-2.4.4.jar:2.4.4]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$9$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:151) [play-netty-server_2.11-2.4.4.jar:2.4.4]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$9$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:148) [play-netty-server_2.11-2.4.4.jar:2.4.4]
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36) [scala-library-2.11.6.jar:na]
at scala.util.Failure$$anonfun$recover$1.apply(Try.scala:215) [scala-library-2.11.6.jar:na]
at scala.util.Try$.apply(Try.scala:191) [scala-library-2.11.6.jar:na]
at scala.util.Failure.recover(Try.scala:215) [scala-library-2.11.6.jar:na]
at scala.concurrent.Future$$anonfun$recover$1.apply(Future.scala:324) [scala-library-2.11.6.jar:na]
at scala.concurrent.Future$$anonfun$recover$1.apply(Future.scala:324) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) [scala-library-2.11.6.jar:na]
at play.api.libs.iteratee.Execution$trampoline$.executeScheduled(Execution.scala:109) [play-iteratees_2.11-2.4.4.jar:2.4.4]
at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:71) [play-iteratees_2.11-2.4.4.jar:2.4.4]
at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:40) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.Promise$DefaultPromise.tryComplete(Promise.scala:248) [scala-library-2.11.6.jar:na]
at scala.concurrent.Promise$class.complete(Promise.scala:55) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.Promise$DefaultPromise.complete(Promise.scala:153) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:23) [scala-library-2.11.6.jar:na]
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) [akka-actor_2.11-2.3.13.jar:na]
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) [akka-actor_2.11-2.3.13.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.6.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.6.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.6.jar:na]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.6.jar:na]
Caused by: java.lang.NullPointerException: null
at com.avaje.ebean.Model$Find.query(Model.java:547) ~[avaje-ebeanorm-4.6.2.jar:na]
at com.avaje.ebean.Model$Find.where(Model.java:741) ~[avaje-ebeanorm-4.6.2.jar:na]
at repositories.AuthRepository.getAuthorizationQuery(AuthRepository.java:12) ~[classes/:na]
at interceptors.AuthInterceptor.call(AuthInterceptor.java:31) ~[classes/:na]
at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:94) ~[play_2.11-2.4.4.jar:2.4.4]
at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:94) ~[play_2.11-2.4.4.jar:2.4.4]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [scala-library-2.11.6.jar:na]
at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.scala:40) ~[play_2.11-2.4.4.jar:2.4.4]
at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:70) [play-iteratees_2.11-2.4.4.jar:2.4.4]
at play.core.j.HttpExecutionContext.execute(HttpExecutionContext.scala:32) ~[play_2.11-2.4.4.jar:2.4.4]
at scala.concurrent.impl.Future$.apply(Future.scala:31) ~[scala-library-2.11.6.jar:na]
at scala.concurrent.Future$.apply(Future.scala:492) ~[scala-library-2.11.6.jar:na]
at play.core.j.JavaAction.apply(JavaAction.scala:94) ~[play_2.11-2.4.4.jar:2.4.4]
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:105) ~[play_2.11-2.4.4.jar:2.4.4]
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:105) ~[play_2.11-2.4.4.jar:2.4.4]
at play.utils.Threads$.withContextClassLoader(Threads.scala:21) ~[play_2.11-2.4.4.jar:2.4.4]
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:104) ~[play_2.11-2.4.4.jar:2.4.4]
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:103) ~[play_2.11-2.4.4.jar:2.4.4]
at scala.Option.map(Option.scala:146) ~[scala-library-2.11.6.jar:na]
at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:103) ~[play_2.11-2.4.4.jar:2.4.4]
at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:96) ~[play_2.11-2.4.4.jar:2.4.4]
at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:524) ~[play-iteratees_2.11-2.4.4.jar:2.4.4]
at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:524) ~[play-iteratees_2.11-2.4.4.jar:2.4.4]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:560) ~[play-iteratees_2.11-2.4.4.jar:2.4.4]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:560) ~[play-iteratees_2.11-2.4.4.jar:2.4.4]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$13.apply(Iteratee.scala:536) ~[play-iteratees_2.11-2.4.4.jar:2.4.4]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$13.apply(Iteratee.scala:536) ~[play-iteratees_2.11-2.4.4.jar:2.4.4]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [scala-library-2.11.6.jar:na]
... 6 common frames omitted
【问题讨论】:
What is a Null Pointer Exception, and how do I fix it?的可能重复 不,我知道 NPE 是什么,我只是不知道为什么会在这种情况下发生。当您在模型中声明一个 Finder 对象时,它应该立即可供调用方法使用。 在这段代码eq("app_id", appId)
,你为什么使用下划线?这看起来更有希望:eq("appId", appId)
.
“在调用 where() 时失败”find
不能不为空,因此它实际上不能专门在该调用 find.where()
上发生:它必须在 where()
内部发生(或者我误解了“在调用 where() 时”的意思。
@GergelyBacso,这是数据库中字段的名称
【参考方案1】:
如果有人遇到类似的问题,应该仔细检查配置。默认的 Model 超类不关心它。有关aplication.conf
和orm.xml
的详细信息可以在这里找到:
EBean config
【讨论】:
感谢您发布此信息!【参考方案2】:在创建静态 Finder 时指定服务器名称对我有用
Finder<String, Site> find = new Finder<String, Site>([servername], Site.class);
【讨论】:
【参考方案3】:我认为您需要使用 com.avaje.ebean.Model.Find 而不是 Model.Finder 类。
public static final Find<Long,ClientAuthorization> find = new Find<Long,ClientAuthorization>();
或者,您可以使用 play.db.ebean.Model.Finder
public static Finder<Long,ClientAuthorization> find = new Finder<Long,ClientAuthorization>( Long.class, ClientAuthorization.class );
我没有测试,请检查并告诉我。请发布异常跟踪以便更好地理解。
【讨论】:
Model.Find 恐怕是一个抽象类。 play Finder 类已弃用。【参考方案4】:稍微浏览一下源代码表明,在您为其构建find
对象的同一类中拥有一个静态初始化的find
对象可能是个坏主意。我会:
前段时间我遇到了一个非常令人难以置信的问题,一个静态字段引用了包含的类类型并产生了意想不到的行为。
【讨论】:
这些选项都不起作用,我仍然得到一个 NPE。我在想我配置模型的方式可能有问题 还是配置?老实说,我只是在猜测...您是否已将模型或其包添加到 applicaton.conf 中的 ebean 服务器?你用 orm.xml 吗? 这是配置,它缺少 ebean 服务器配置。不知道为什么模板安装中会缺少该配置。如果您对此做出另一个答案,我会将其标记为正确 啊。重要的是它最终被修复了。 当然。绝对是我遇到的最奇怪的事情。我认为它是内置于 2.4 的(尽管我曾怀疑这是某个问题),但我想我错了。我会再次检查模板安装,看看它是否在那里。如果没有,可能值得向 typesafe 开一张票,告诉他们默认包含配置以上是关于Play 2.4 Finder 抛出空指针异常的主要内容,如果未能解决你的问题,请参考以下文章