Unexplainable ClassCastException thrown in Play Framework
Asked Answered
T

3

7

I've been experiencing a strange error while working on my Play Framework project. While my project is running, I will sometimes receive a ClassCastException, but the error is this:

ClassCastException occured : models.Person cannot be cast to models.Person

This occurs usually when I'm calling a find method such as:

Person p = Person.find("name=?","Joe").first();

If I restart the project, the problem goes away, but only temporarily. It makes testing my project a major pain. How do I fix this?

Teresa answered 10/12, 2011 at 18:0 Comment(3)
I am facing the same problem. I believe this is something related to the "reload" feature of the dev webserver. It appears to be broken or my machine is bad configurated. Since Play is too imature yet, I bet in the first option.Drews
I have the same problem in play 2.6.3.Glissando
As @HotLicks mentioned this can be a namespace issue. I hadn't noticed that I actually had two main.scala.html files (one in a sub-project) which was throwing me this runtime error - so changing that filename made it all go awayJunji
I
3

I've experienced this error while in Dev mode in Play, in two scenarios (as far as I can remember):

  • Modify an entity and try to recover values from cache that are objects of that entity class.
  • A compilation error while reloading the code of the page/application

In both scenarios fixing compilation errors or cleaning the cache solved the issue.

Not saying that those are the only possibilities, it may be that you are having some other issue.

Infanticide answered 11/12, 2011 at 19:19 Comment(1)
I've seen this a few times in dev mode also. It usually happens after a hot code change, even if made to files unrelated to the entity (the Person class in your case.)Laforge
T
3

This most likely occurs because you've somehow loaded the Person class under two different class loaders. When a class is loaded twice in two different class loaders it's effectively two distinct classes.

(Unfortunately, I can't tell you where/how you might have done this.)

(And it is a bit curious to have the problem pop up on the statement you list. Are you certain that's where it's occurring? Perhaps you should show the exception traceback.)

Twum answered 10/12, 2011 at 18:13 Comment(2)
I'm pretty sure this is where it happens-- when running the project on my local workspace, the Play error page will appear and it highlights this statement in red. This error usually occurs when I make an edit to my code, save the file, and then go back to my project.Teresa
@Teresa -- If you're dynamically editing/compiling while running, that would explain it.Twum
I
3

I've experienced this error while in Dev mode in Play, in two scenarios (as far as I can remember):

  • Modify an entity and try to recover values from cache that are objects of that entity class.
  • A compilation error while reloading the code of the page/application

In both scenarios fixing compilation errors or cleaning the cache solved the issue.

Not saying that those are the only possibilities, it may be that you are having some other issue.

Infanticide answered 11/12, 2011 at 19:19 Comment(1)
I've seen this a few times in dev mode also. It usually happens after a hot code change, even if made to files unrelated to the entity (the Person class in your case.)Laforge
N
1

In my case, this is related to applying evolutions from the web interface. Someone raised a bug for this, but so far it hasn't received any attention from the dev team. There is a patch attached to the ticket, but I haven't tried it, so YMMV.

Neurologist answered 4/11, 2013 at 11:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.