0

I'm currently having an issue with a Finder instance in Play 2.4 whereby it returns a null pointer exception when used. 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. 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

update

Since it came up in a comment, this is not just a question of what a NPE is, I know that already. 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
Zarathuztra
  • 3,215
  • 1
  • 20
  • 34
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – ΦXocę 웃 Пepeúpa ツ Nov 26 '15 at 14:07
  • Nah, I know what an NPE is, I just can't figure out why it's happening in this instance. When you declare a Finder object in a Model, it should be immediately available to a calling method. – Zarathuztra Nov 26 '15 at 14:07
  • 1
    In this code `eq("app_id", appId)`, why are you using underscores? This looks more promising: `eq("appId", appId)` . – Gergely Bacso Nov 26 '15 at 14:16
  • "It fails on the call to where()" `find` cannot be not null, so it cannot actually be occurring specifically on that call `find.where()`: it must be happening inside `where()` (or I've misunderstood what you mean by "on the call to where()"). – Andy Turner Nov 26 '15 at 14:27
  • @GergelyBacso, thats the name of the field in the database – Zarathuztra Nov 26 '15 at 16:36
  • @AndyTurner, you've got it. Something is happening inside where() that is throwing the NPE. I'm still investigating – Zarathuztra Nov 26 '15 at 16:38
  • @Zarathuztra posting the stack trace would help. – Andy Turner Nov 26 '15 at 16:39
  • @AndyTurner sure, posting now – Zarathuztra Nov 26 '15 at 16:46

4 Answers4

1

In case somebody runs into a similar problem, the config should be double-checked. The default Model superclass does not care of it. Details about aplication.conf and orm.xml can be found here:

EBean config

Gergely Bacso
  • 14,243
  • 2
  • 44
  • 64
1

Specifying the server name when creating the static Finder worked for me

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

I think you need to use com.avaje.ebean.Model.Find instead of Model.Finder class.

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

Alternatively, you can use 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.

gpothan
  • 151
  • 7
0

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. 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.

Gergely Bacso
  • 14,243
  • 2
  • 44
  • 64
  • Neither of those options worked, I still get an NPE. I'm thinking there may be something wrong with how I configured the model – Zarathuztra Nov 26 '15 at 17:21
  • Or the configuration? I am just guessing to be honest... Do you have the model or its package added to an ebean server in applicaton.conf? Do you use orm.xml? – Gergely Bacso Nov 26 '15 at 18:09
  • It was the config, it was missing the ebean server config. No idea why that config would be missing in the template install. If you make another answer with that I'll mark it correct – Zarathuztra Nov 27 '15 at 12:24
  • Ahh. The important is that it is fixed finally. – Gergely Bacso Nov 27 '15 at 12:56
  • For sure. Definitely the weirdest thing I've run into. I thought it was built in to 2.4 (although I had suspected this was the issue at some point) but I guess I was wrong. I'll check the template install again and see if it's there. If not, might be worth opening a ticket to typesafe to tell them to include the config by default – Zarathuztra Nov 27 '15 at 13:10