我正在使用Scala Liftweb并拥有此模型对象:
object Product extends Product with LongKeyedMetaMapper[Product] {
override def dbTableName = "products"
override def dbIndexes = UniqueIndex(slug) :: super.dbIndexes
def menus = sitemap
lazy val sitemap: List[Menu] = List(editProductMenuLoc, listProductsMenuLoc, createProductMenuLoc, indexProductsMenuLoc).flatten(a => a)
protected def editProductMenuLoc =
Full(Menu(Loc("EditProduct" + menuNameSuffix, editPath, S.?("edit.product"))))
protected def listProductsMenuLoc = Full(Menu(Loc("ListProduct" + menuNameSuffix, listPath, S.?("list.products"))))
protected def indexProductsMenuLoc = Full(Menu(Loc("ListProduct" + menuNameSuffix, indexPath, S.?("index.products"))))
protected def createProductMenuLoc =
Full(Menu(Loc("CreateProduct" + menuNameSuffix, createPath, S.?("create.product"))))
protected val menuNameSuffix = ""
protected val editSuffix = "edit"
protected val createSuffix = "create"
protected val viewSuffix = "view"
protected val editPath = theAdminPath(editSuffix)
protected val createPath = theAdminPath(createSuffix)
protected val viewPath = thePath(viewSuffix)
protected val listPath = basePath
protected val indexPath = adminPath
protected def thePath(end: String): List[String] = basePath ::: List(end)
protected def theAdminPath(end: String): List[String] = adminPath ::: List(end)
protected val basePath: List[String] = "products" :: Nil
protected val adminPath: List[String] = "admin" :: "products" :: Nil
}
当我编译时,它工作正常,一旦我尝试运行它,我得到这个错误:
Caused by: java.lang.NullPointerException: null
at scala.collection.immutable.List.$colon$colon$colon(List.scala:120) ~[scala-library.jar:0.12.2]
at code.model.Product$.theAdminPath(Product.scala:65) ~[classes/:na]
at code.Product$.<init>(Product.scala:53) ~[classes/:na]
at code.Product$.<clinit>(Product.scala) ~[classes/:na]
... 49 common frames omitted
我已经在MegaProtoMetaUser
源代码中找到的代码之后对这些路径进行了建模,我不知道为什么这里会出现空指针异常 - 所有值都已正确填充,不是吗?
答案 0 :(得分:5)
问题是字段的初始化顺序,从上到下。这意味着在basePath和adminPath之前初始化editPath,createPath和viewPath。由于这些前一个字段在初始化基本路径之前调用方法thePath和theAdminPath,因此对这些方法的调用使用basePath和adminPath - null的预初始化值。尝试将这两个字段的定义移动到调用涉及它们的方法的任何字段的定义之上。