0

What are Class Cast Exceptions (java.lang.ClassCastException) and what causes them?

What are some ways to find and fix the cause of the exception so the code compiles and runs?

Josh Heaps
  • 335
  • 1
  • 8
  • Thank you! I looked for something like that, but couldn't find it. That helps a ton. – Josh Heaps Mar 08 '17 at 04:37
  • What is it? Apples can not be cast as Oranges, they are different (incompatible) types (although they are both fruit). *"What are some ways to find"* - Read the stack trace, it should highlight the file and line number where the error occurred. *"fix the cause of the exception"* - Don't do blind casts, use `instanceof` to determine if a cast will work or not – MadProgrammer Mar 08 '17 at 04:37
  • https://examples.javacodegeeks.com/java-basics/exceptions/java-lang-classcastexception-how-to-solve-class-cast-exception/ – deen Mar 08 '17 at 04:38
  • @JoshHeaps You may wish to refer this link. http://www.buggybread.com/2014/06/error-exception-in-thread.html – Pratik Ambani Mar 08 '17 at 04:38
  • Okay, so It's an inheritance/polymorphism problem then. – Josh Heaps Mar 08 '17 at 04:38

0 Answers0