我有2个具有相同表的foriegn密钥,它解析到程序中得到编译错误 问题是 2 Id's is null
模特班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
}
}
模型UserProfile和函数
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)
}
}
})
}
创建MyFriend Page功能
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")
})
}
}
重定向到myFriend页面
def myFriend = Action { implicit request =>
Ok(views.html.myFriend(Application.myFriendForm,userprofile,myfriend))
}
当程序得到id的
的nullpointer时如果有任何人遇到同样的问题且任何人都解决了,我会在几天内遇到这个问题
答案 0 :(得分:1)
只需添加到模式匹配构造中。
case None => // do some if None
或
case _ => // something in this case
或者你可以通过省略箭头的右侧来忽略这种情况
问题是你没有涵盖你职能中的所有可能案例。您已涵盖Some (authuserProfile:UserProfile)
和case Some(authmyfriend: MyFriend)
,但不是无案例,看起来您的方法收到无。
<强>更新强>
发生错误的原因是您有_
符合所有情况,因此模式匹配构造在case _ => //...
如果我理解你的代码是正确的,那么问题在于这部分
userProfileOpt match {
case _ => Ok(views.html.myFriend(...).withSession(friendSession)
case _ =>
val myFriendFound: UserProfile=null
val useracc:UserAccount=null
这两种情况都会混淆所有可能的情况,这是一个错误
答案 1 :(得分:0)
UserProfile.myFriend
返回MyFriend
,而不是Option[MyFriend]
。因此,您的模式匹配不正确。您可以尝试修复def myFriend
以返回Option
,或删除模式匹配。
(编辑):您可能应该从MyFriend.simple.single
更改为MyFriend.simple.singleOpt
。
根据经验,始终使用公共方法的显式返回类型。类型推断是非常强大和有用的,但明智地使用它。