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

2019-08-16 05:36发布

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条回答
乱世女痞
2楼-- · 2019-08-16 06:07

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.

查看更多
登录 后发表回答