简体   繁体   English

播放2.4 Finder引发Null指针异常

[英]Play 2.4 Finder throws Null Pointer Exception

I'm currently having an issue with a Finder instance in Play 2.4 whereby it returns a null pointer exception when used. 我目前在Play 2.4中遇到Finder实例的问题,该实例在使用时会返回空指针异常。 Here is the code trying to use it 这是尝试使用它的代码

public ExpressionList<ClientAuthorization> getAuthorizationQuery(String appId) {
        return ClientAuthorization.find.where().eq("app_id", appId).eq("active", 1);
    }

And the model class 和模特班

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


}

This works fine for every model I've ever created in Play, so I'm not sure what's going on. 这对于我在Play中创建的每个模型都适用,所以我不确定发生了什么。 Here is what I get 这就是我得到的

NPE

I greatly appreciate any help I can get. 我非常感谢我能得到的任何帮助。 This is driving me crazy in an upgrade project I'm working on. 这使我在进行的升级项目中疯狂。 The last upgrade I did went just fine, but this one is giving me problems. 我所做的最后一次升级进展顺利,但这给我带来了问题。

Additional info: It fails on the call to where() just after the ClientAuthorization static call to find 附加信息:在ClientAuthorization静态调用查找之后,调用where()失败

update 更新

Since it came up in a comment, this is not just a question of what a NPE is, I know that already. 由于它是在评论中提出的,所以这不仅仅是NPE是什么的问题,我已经知道了。 For some reason the finder is not initialized. 由于某种原因,查找器未初始化。 However, looking at this model, and every model I've ever written, there is no reason for this to be happening. 但是,查看此模型以及我曾经编写的每个模型,都没有发生这种情况的原因。

Here is the stack trace 这是堆栈跟踪

@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

In case somebody runs into a similar problem, the config should be double-checked . 万一有人遇到类似问题, 则应仔细检查配置 The default Model superclass does not care of it. 默认的Model超类不关心它。 Details about aplication.conf and orm.xml can be found here: 有关详细信息aplication.conform.xml可以在这里找到:

EBean config EBean配置

创建静态Finder时指定服务器名称对我有用

Finder<String, Site> find = new Finder<String, Site>([servername], Site.class);

I think you need to use com.avaje.ebean.Model.Find instead of Model.Finder class. 我认为您需要使用com.avaje.ebean.Model.Find而不是Model.Finder类。

 public static final Find<Long,ClientAuthorization> find = new Find<Long,ClientAuthorization>(){};

Alternatively, you can use play.db.ebean.Model.Finder 另外,您可以使用play.db.ebean.Model.Finder

public static Finder<Long,ClientAuthorization> find = new Finder<Long,ClientAuthorization>(  Long.class, ClientAuthorization.class ); 

I have not tested, please check and let me know. 我尚未测试,请检查并告知我。 Please post the Exception trace for better understanding. 请发布异常跟踪以更好地理解。

Looking through the source code a bit suggests that having a staticly initialized find object within the same class that you are building the find object for might be a bad idea. 仔细查看源代码find ,在要为其创建对象的同一类中拥有一个静态初始化的find对象可能不是一个好主意。 I would: 我会:

  • first completey remove this variable from the model class and create that field somewhere else. 首先,从模型类中完全删除此变量,然后在其他地方创建该字段。
  • or (if you must have it there) initialize that object lazily when it is first requested. 或(如果您必须在那里)在第一次请求该对象时会延迟进行初始化。

I have had a very mind-boggling issue some time ago with a static field referencing the encompassing class type and producing unexpected behaviour. 前段时间我遇到了一个令人难以置信的问题,其中一个静态字段引用了所包含的类类型并产生了意外的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM