Cast Class Exception in Scala: java.lang.Object to Scala CaseClass

ⅰ亾dé卋堺 提交于 2019-12-11 18:54:46

问题


This is driving me crazy - I'm working on play with scala, social secure etc and I keep getting this error when I'm trying to make a case class.

Edit: similar question to this: Scala and Play2: ClassCastException: java.lang.Object cannot be cast to play.api.libs.json.JsValue

Edit: moving the method createIdentityFromUser to the static companion class seems to fix this. Not sure why.

[ClassCastException: java.lang.Object cannot be cast to securesocial.core.SocialUser] 
In /Users/tripled153/Development/src/Foundation/playApp/app/service/SecureSocialUserService.scala     at line 58.
54//    }
55
56    println("here2")
57    //val ret = user.map(_.createIdentityFromUser)
58    user.foreach(x => x.createIdentityFromUser)
59    user match{
60      case Some(x) => Some(x.createIdentityFromUser)
61      case None => None

Line42 errors out. The map, foreach, and match all bomb with that classcastexception and I don't understand why.

Here is the code cleaned up a bit for readability (note SocialUser extends Identity trait):

  def find(id: UserId): Option[Identity] = {
   if ( Logger.isDebugEnabled ) {
     Logger.debug("find users = %s".format(users))
   }

   //Option[GraphedUser]
   val user = GraphedUser.getUser(id.id)

   //ret should be Option[SocialUser]
   val ret = user.map(_.createIdentityFromUser) //errors!

   return ret

 }

And the method that is called to produce the object which appears to be the problem. It's in a trait that extends tinkerpop frames VertexFrame interface. I'm guessing that has something to do with it.

  def createIdentityFromUser: SocialUser = {

    return new SocialUser(new UserId(getUserId, getProviderId), getFirstName, getLastName, getName, Some(getEmail),
      None, new AuthenticationMethod("userPassword"), None, None, Some(new PasswordInfo(getPasswordInfoHash, getPasswordInfoSalt)))

  }

回答1:


I made the method static taking a parameter and it works... No idea why but it solved the problem. In java this would not come up.



来源:https://stackoverflow.com/questions/14541682/cast-class-exception-in-scala-java-lang-object-to-scala-caseclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!