gpt4 book ai didi

scala - 从数据模型创建数据表时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-03 00:11:09 24 4
gpt4 key购买 nike

我使用slick2.0和postgresql数据库:

我的数据模型是:

class User(tag: Tag) extends Table[(Int, String,String, String,Date,String,Long,   Int)](tag, "User") {
def id = column[Int]("ID", O.PrimaryKey)
def first_name = column[String]("First_Name")
def second_name=column[String]("Second_Name")
def email=column[String]("Email")
def datebirth=column[Date]("Birth_date")
def password=column[String]("Password")
def addID = column[Long]("ADRESS")
// def shopID =column[Int]("Shop")
def privilege=column[Int]("privilege")
def * = (id, first_name, second_name, email, datebirth, password, addID, privilege)
// def home_address=foreignKey("ha_FK", addID, address)(_.id)
// def link_shop=foreignKey("sh_FK", shopID, shop)(_.id)
}
val user=TableQuery[User]

在我的 Global.scala 上:

(user.ddl).create          -----line 67

错误是:

 play.api.UnexpectedException: Unexpected exception[NullPointerException: null]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anon
fun$1.apply(ApplicationProvider.scala:148) ~[play_2.10-2.2.1.jar:2.2.1]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anon
fun$1.apply(ApplicationProvider.scala:112) ~[play_2.10-2.2.1.jar:2.2.1]
at scala.Option.map(Option.scala:145) ~[scala-library-2.10.3.jar:na]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply
(ApplicationProvider.scala:112) ~[play_2.10-2.2.1.jar:2.2.1]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply
(ApplicationProvider.scala:110) ~[play_2.10-2.2.1.jar:2.2.1]
at scala.util.Success.flatMap(Try.scala:200) ~[scala-library-2.10.3.jar:
na]
Caused by: java.lang.NullPointerException: null
at scala.slick.profile.RelationalProfile$TableQueryExtensionMethods.ddl(
RelationalProfile.scala:41) ~[slick_2.10-2.0.1.jar:2.0.1]
at InitialData$$anonfun$insert$1.apply(Global.scala:67) ~[na:na] ------------here is the user.dll.create and here is the error.
at InitialData$$anonfun$insert$1.apply(Global.scala:61) ~[na:na]
at scala.slick.backend.DatabaseComponent$DatabaseDef$class.withSession(D
atabaseComponent.scala:31) ~[slick_2.10-2.0.1.jar:2.0.1]
at scala.slick.jdbc.PlayDatabase.withSession(PlayDatabase.scala:6) ~[pla
y-slick_2.10-0.6.0.1.jar:2.0.1]
at InitialData$.insert(Global.scala:60) ~[na:na]

为什么当我创建数据表时出现错误,出现 NullPointerException

最佳答案

如果您更改 TableQuery 声明,这看起来是初始化顺序问题:

val user=TableQuery[User]

至:

def user=TableQuery[User]

这应该可以解决问题。

关于scala - 从数据模型创建数据表时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22937541/

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