I am using Scala Liftweb and have this model object:

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
}

When I compile, it works fine, once I try to run it, I get this error:

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

I have modelled these paths after the code I found in the MegaProtoMetaUser source, and I have no idea why there would be a null pointer exception here - all values are properly filled, aren't they?

有帮助吗?

解决方案

The problem is the order in which the fields are initialised, which is top to bottom. This means that editPath, createPath, and viewPath are being initialised before basePath and adminPath. Since these former fields call the methods thePath and theAdminPath before the base paths have been initialised, the calls to these methods use the pre-initialised values of basePath and adminPath - null. Try moving the definitions of these two fields above the definitions of any field that calls methods involving them.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top