javaplayframeworknullpointerexceptionpersistenceebean

Play Framework app constantly throws a null pointer on line 6


I'm trying to make a simple forum system in Play Framework, but I'm getting an error in a strange way. I have a forum.java class which is the source of the error, but regardless of the code inside, it always throws a null pointer on line 6. Here's as much as is relevant, I think.

package models;

import io.ebean.Finder;
import io.ebean.Model;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import java.util.List;

@Entity
public class Forum extends Model {

    @Id
    private String id;

    @ManyToOne(cascade = CascadeType.ALL)
    private List<Thread> threads;

The offending line is the javax.persistence.CascadeType import, but I've already removed it, shifted it downwards, remade the class. Regardless, the error continues to throw on line 6. It doesn't explain what's causing the null pointer, either. It even throws when the line is empty.

For reference, I have plenty of other classes that are laid out in the same format. I've tried to clean the project in the sbt console and I've tried remaking the class from the ground up. I'm sure it's some kind of fundamental error, but I have no idea where to even start.

Edit: Stacktrace added

[info] play.api.Play - Application started (Dev)
[error] application -

! @77hm04h99 - Internal server error, for (GET) [/forum/1] ->

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[NullPointerException: null]]
        at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:255)
        at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:182)
        at play.core.server.AkkaHttpServer$$anonfun$2.applyOrElse(AkkaHttpServer.scala:320)
        at play.core.server.AkkaHttpServer$$anonfun$2.applyOrElse(AkkaHttpServer.scala:318)
        at scala.concurrent.Future.$anonfun$recoverWith$1(Future.scala:414)
        at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)
        at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
        at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:91)
        at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
Caused by: java.lang.NullPointerException: null
        at io.ebeaninternal.server.deploy.BeanPropertyAssocMany.createReference(BeanPropertyAssocMany.java:652)
        at io.ebeaninternal.server.deploy.BeanDescriptor.lazyLoadMany(BeanDescriptor.java:2082)
        at io.ebeaninternal.server.deploy.BeanDescriptor.lazyLoadMany(BeanDescriptor.java:2071)
        at io.ebeaninternal.server.loadcontext.DLoadBeanContext$LoadBuffer.loadBean(DLoadBeanContext.java:172)
        at io.ebean.bean.EntityBeanIntercept.loadBeanInternal(EntityBeanIntercept.java:812)
        at io.ebean.bean.EntityBeanIntercept.loadBean(EntityBeanIntercept.java:785)
        at io.ebean.bean.EntityBeanIntercept.preGetter(EntityBeanIntercept.java:885)
        at models.Forum._ebean_get_threads(Forum.java:6)
        at models.Forum.getThreads(Forum.java:37)
        at views.html.forum$.apply(forum.template.scala:41)

Stacktrace v2 :

[error] application -

! @77hpce3hh - Internal server error, for (GET) [/forum/1] ->

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[PersistenceException: Query threw SQLException:Column "T1.FORUM_ID" not found; SQL statement:
select t0.id, t1.id, t1.title, t1.replies, t1.last_reply, t1.forum_id from forum t0 left join thread t1 on t1.forum_id = t0.id  where t0.id = ?   order by t0.id [42122-196] Bind values:[null] Query was:select t0.id, t1.id, t1.title, t1.replies, t1.last_reply, t1.forum_id from forum t0 left join thread t1 on t1.forum_id = t0.id  where t0.id = ?   order by t0.id]]
        at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:255)
        at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:182)
        at play.core.server.AkkaHttpServer$$anonfun$2.applyOrElse(AkkaHttpServer.scala:320)
        at play.core.server.AkkaHttpServer$$anonfun$2.applyOrElse(AkkaHttpServer.scala:318)
        at scala.concurrent.Future.$anonfun$recoverWith$1(Future.scala:414)
        at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)
        at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
        at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:91)
        at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
Caused by: javax.persistence.PersistenceException: Query threw SQLException:Column "T1.FORUM_ID" not found; SQL statement:
select t0.id, t1.id, t1.title, t1.replies, t1.last_reply, t1.forum_id from forum t0 left join thread t1 on t1.forum_id = t0.id  where t0.id = ?   order by t0.id [42122-196] Bind values:[null] Query was:select t0.id, t1.id, t1.title, t1.replies, t1.last_reply, t1.forum_id from forum t0 left join thread t1 on t1.forum_id = t0.id  where t0.id = ?   order by t0.id
        at io.ebean.config.dbplatform.SqlCodeTranslator.translate(SqlCodeTranslator.java:48)
        at io.ebean.config.dbplatform.DatabasePlatform.translate(DatabasePlatform.java:195)
        at io.ebeaninternal.server.query.CQueryEngine.translate(CQueryEngine.java:139)
        at io.ebeaninternal.server.query.DefaultOrmQueryEngine.translate(DefaultOrmQueryEngine.java:38)
        at io.ebeaninternal.server.core.OrmQueryRequest.translate(OrmQueryRequest.java:81)
        at io.ebeaninternal.server.query.CQuery.createPersistenceException(CQuery.java:666)
        at io.ebeaninternal.server.query.CQueryEngine.find(CQueryEngine.java:422)
        at io.ebeaninternal.server.query.DefaultOrmQueryEngine.findId(DefaultOrmQueryEngine.java:162)
        at io.ebeaninternal.server.core.OrmQueryRequest.findId(OrmQueryRequest.java:327)
        at io.ebeaninternal.server.core.DefaultServer.findId(DefaultServer.java:1175)
Caused by: org.h2.jdbc.JdbcSQLException: Column "T1.FORUM_ID" not found; SQL statement:
select t0.id, t1.id, t1.title, t1.replies, t1.last_reply, t1.forum_id from forum t0 left join thread t1 on t1.forum_id = t0.id  where t0.id = ?   order by t0.id [42122-196]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
        at org.h2.message.DbException.get(DbException.java:179)
        at org.h2.message.DbException.get(DbException.java:155)
        at org.h2.expression.ExpressionColumn.optimize(ExpressionColumn.java:147)
        at org.h2.command.dml.Select.prepare(Select.java:839)
        at org.h2.command.Parser.prepareCommand(Parser.java:263)
        at org.h2.engine.Session.prepareLocal(Session.java:578)
        at org.h2.engine.Session.prepareCommand(Session.java:519)
        at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1204)
        at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:73)

Solution

  • The line

    at io.ebeaninternal.server.deploy.BeanPropertyAssocMany.createReference(BeanPropertyAssocMany.java:652)
    

    Can help you understand what went wrong here:

    You have misused the @ManyToOne annotation.

    In your application, you have one Forum for many Threads, so the annotation above the list of Threads inside your Forum Model should be

    @OneToMany
    

    You also need to switch the annotation above the declaration of Forum in your Thread Model to

    @ManyToOne