0

我有 2 个具有同一个表的外键,它解析到程序中得到一个编译错误问题是2 个 ID 为空

模型类 MyFriend 和函数

case class MyFriend(id: Pk[Long]= NotAssigned,user_Id:Option[Long],friend_Id:Option[Long],is_accepted:Boolean)
object MyFriend{
  /**
   * parse a Myfreind from a ResultSet.
   */
  val simple ={
    get[Pk[Long]]("myfriend.id") ~
    get[Option[Long]]("myfriend.user_Id")~
    get[Option[Long]]("myfriend.friend_Id")~
    get[Boolean]("myfriend.is_accepted") map{
      case id ~ user_Id ~ friend_Id ~ is_accepted => MyFriend(id, user_Id,friend_Id,is_accepted)
    }
   }


 /**
   * Parse a MyFriend from a userProfile
   */
  val withUserProfile =MyFriend.simple ~ (UserProfile.simple ?) map{
    case myfriend  ~ userprofile => (myfriend, userprofile)
  }
  /**
   * create a new MyFriend.
   *
   * @param myfriend 
   */
  def insert(myFriend: MyFriend): Long = {
    DB.withConnection { implicit connection =>
      SQL(
        """
          insert into MY_FRIEND(USER_ID,FRIEND_ID,IS_ACCEPTED) values (
            {user_Id}, {friend_Id},{is_accepted}
          )
        """).on(
          'user_Id -> myFriend.user_Id,
          'friend_Id -> myFriend.friend_Id,
          'is_accepted -> myFriend.is_accepted
          ).executeUpdate()
    }
  }


/**
   * Update a MyFriend
   *
   * @param  MyFriend
   */
  def update(myFriend:MyFriend)={
  DB.withConnection{ implicit connection =>
    SQL(
        """
        update MY_FRIEND
        set FRIEND_ID = {friend_Id}, IS_ACCEPTED={is_accepted} where USER_ID={use_id}
        """).on(
          'user_Id -> myFriend.user_Id,
          'friend_Id -> myFriend.friend_Id,
          'is_accepted -> myFriend.is_accepted
          ).executeUpdate()

  }

}
/**
   * Find myfriendId Via userprofile
   */
  def authenticate(myFriend: MyFriend) = {
    DB.withConnection { implicit connection =>
      val myFriendFound = SQL(
        """
          select * from MY_FRIEND 
          where USER_ID = {user_Id} and FRIEND_ID={friend_Id}
        """).on(
          'user_Id -> myFriend.user_Id,
          'friend_Id ->myFriend.friend_Id
          ).as(MyFriend.simple.singleOpt)
      myFriendFound
    }
  }

模型用户配置文件和函数

case class UserProfile(id: Pk[Long] = NotAssigned, useraccountid: Option[Long], name: String, date_of_birth: Date, gender: String, image: String,status:String)

object UserProfile{

  /**
   * Parse a UserProfile from a ResultSet
   */
  val simple = {
    get[Pk[Long]]("user_profile.id") ~
      get[Option[Long]]("user_profile.user_account_id") ~
      get[String]("user_profile.name") ~
      get[Date]("user_profile.date_of_birth") ~
      get[String]("user_profile.gender") ~
      get[String]("user_profile.image") ~
      get[String]("user_profile.status") map {
        case id ~ user_account_id ~ name ~ date_of_birth ~ gender ~ image ~ status =>
          UserProfile(id, user_account_id, name, date_of_birth, gender, image,status )
      }
  }
 /**
   * Parse a userProfile from a MyFriend
   */
  val withMyFriend =UserProfile.simple ~ (MyFriend.simple ?) map{
    case userprofile  ~ myfriend  => (userprofile, myfriend)
  }
/**
   * Find MyFriend With MyFriend Detail
   */

  def myFriend(user_Id:Long) = {
    DB.withConnection { implicit connection =>
      val myFriend = SQL(
        """
          select * from MY_FRIEND
          where USER_ID = {user_id}
        """).on(
          'user_Id -> user_Id).as(MyFriend.simple.singleOpt)
      myFriend
    }
  }
  /**
   * Authonticate
   */
  def authenticate(userprofile: UserProfile) = {
    DB.withConnection { implicit connection =>
      val userProfileFound = SQL(
        """
          select * from USER_PROFILE 
          where ID = (id}
        """).on(
          'Id -> userprofile.id
          ).as(UserProfile.simple.singleOpt)
      userProfileFound
    }
  }

用于解析朋友和用户 ID 的控制器应用程序方法

val userprofile:UserProfile=null
        val myfriend:MyFriend=null

   def authenticateFriend = Action { implicit request =>
            val alert: Alert = new Alert("", "")
            Common.setAlert(alert)
            myFriendForm.bindFromRequest.fold(
            errors => BadRequest(views.html.myFriend(errors,userprofile,myfriend)),
        myFriend => {
            val myfriendOpt = MyFriend.authenticate(myFriend)
        myfriendOpt match {

       case Some(authmyfriend: MyFriend) =>
            val userSession = request.session + ("myFriendId" -> authmyfriend.id.toString)
            val friendSession=request.session + ("userProfileId" -> userprofile.id.toString)
            val myFriendOpt = MyFriend.userProfile(authmyfriend.id.get)
       myFriendOpt match {
       case None => Ok(views.html.myFriend(Application.myFriendForm, userprofile,myfriend)).withSession(userSession)
       case Some(userProfileFound: UserProfile) =>
            val myFriendFormWithDetails = Application.myFriendForm.fill(userProfileFound)
            Ok(views.html.myFriend(myFriendFormWithDetails,userprofile,authmyfriend)).withSession(userSession)

            }

          }
      })

  }

创建我的朋友页面功能

     def createMyFriend = Action { implicit request =>
   if (request.session.get("userId") == None) {
      Results.Redirect("/")
    }
   else {
        val myfriends:MyFriend=null
        val userprofileId = request.session.get("myFriendId").get.toLong//userProfileId
        val userprofile = UserProfile.findUserByAccountId(userprofileId).get
        println(userprofile)
        val myfriendId = request.session.get("userProfileId").get.toLong//myFriendId
        val myfriend = MyFriend.friendidByUserIsAccepted(myfriendId,true)
        println(myfriend)
        myFriendForm.bindFromRequest.fold(
        errors => BadRequest(views.html.myFriend(errors, userprofile,myfriends)),
   myFriend => {
          println("errors")
          val myFriendOpt = UserProfile.myFriend(userprofile.id.get)
          println(myFriendOpt)
   myFriendOpt match {
   case None =>
          val updatedMyFriend = MyFriend(NotAssigned,
          Option(userprofileId), Option(myfriendId),myFriend.is_accepted)
          MyFriend.insert(updatedMyFriend)
          val alert: Alert = new Alert("success", " MyFriend Saved")
          Common.setAlert(alert)
          }
          Results.Redirect("/myFriend")
        })  
        }
    }        

重定向到我的朋友页面

def myFriend = Action { implicit request =>

   Ok(views.html.myFriend(Application.myFriendForm,userprofile,myfriend))
    }

运行程序时获取 id 的空指针

如果有人遇到同样的问题并且有人解决了,我会在几天内遇到这个问题

4

2 回答 2

1

只需添加到您的模式匹配结构。

case None => // do some if None 

或者

case _ => // something in this case

或者您可以通过省略箭头的右侧来简单地忽略这种情况

问题是您没有涵盖您功能中所有可能的情况。您已经介绍了Some (authuserProfile:UserProfile)and case Some(authmyfriend: MyFriend),但没有介绍 None 的情况,看起来您的方法接收到 None。

更新

发生错误是因为你有_匹配所有情况的,所以你的模式匹配构造在之后无法达到任何东西(在你的情况下)case _ => //...

如果我理解你的代码是正确的,那么问题就出在这部分

userProfileOpt match {
  case _ => Ok(views.html.myFriend(...).withSession(friendSession)
  case _ =>
    val myFriendFound: UserProfile=null
    val useracc:UserAccount=null

这两种情况都计算了所有可能的情况,这是一个错误

于 2013-05-29T06:30:30.400 回答
0

UserProfile.myFriend回报MyFriend,不是Option[MyFriend]。因此,您的模式匹配不正确。您可以尝试修复def myFriendto return Option,或删除模式匹配。

(编辑):可能你应该从 更改MyFriend.simple.singleMyFriend.simple.singleOpt

根据经验,始终使用公共方法的显式返回类型。类型推断非常强大和有用,但要明智地使用它。

于 2013-05-29T09:37:59.547 回答