gpt4 book ai didi

java - Play Framework 应用程序在第 6 行不断抛出空指针

转载 作者:行者123 更新时间:2023-12-02 11:23:03 25 4
gpt4 key购买 nike

我正在尝试在 Play Framework 中制作一个简单的论坛系统,但我收到了一个奇怪的错误。我有一个 forum.java 类,它是错误的根源,但无论里面的代码如何,它总是在第 6 行抛出一个空指针。我认为,这就是相关的内容。

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;

有问题的行是 javax.persistence.CascadeType 导入,但我已经删除了它,将其向下移动,重新创建了类。不管怎样,错误继续在第 6 行抛出。它也没有解释导致空指针的原因。当该行为空时它甚至会抛出异常。

作为引用,我还有很多以相同格式布局的其他类。我尝试在 sbt 控制台中清理该项目,并尝试从头开始重新创建该类。我确信这是某种根本性错误,但我不知道从哪里开始。

编辑:添加了堆栈跟踪

[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)

堆栈跟踪 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)

最佳答案

线路

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

可以帮助您了解这里出了什么问题:

您滥用了@ManyToOne注释。

在您的应用程序中,您有一个论坛用于许多线程,因此列表上方的注释论坛模型中的线程应该是

@OneToMany

您还需要将 Thread 模型中 Forum 声明上方的注释切换为

@ManyToOne

关于java - Play Framework 应用程序在第 6 行不断抛出空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49792449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com