1

我正在使用 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源代码中找到的代码之后对这些路径进行了建模,但我不知道为什么这里会出现空指针异常 - 所有值都已正确填充,不是吗?

4

1 回答 1

5

问题是字段初始化的顺序,从上到下。这意味着 editPath、createPath 和 viewPath 在 basePath 和 adminPath 之前被初始化。由于这些以前的字段在基本路径初始化之前调用方法 thePath 和 theAdminPath,因此对这些方法的调用使用预初始化的 basePath 和 adminPath 值 - null。尝试将这两个字段的定义移到调用涉及它们的方法的任何字段的定义之上。

于 2013-08-29T22:28:20.510 回答