Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cannot be cast to String error

 
Ivan Turner
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, In the code below, I had to put try/catch block to keep from getting an error. The e1.toString() error is "java.lang.ClassCastException: org.apache.catalina.core.DispatchTargetsInfo cannot be cast to java.lang.String" I thought any item in an Enumeration could be cast to a string. I know I'm using deprecated attributes on the tags since I'm only trying to learn principles at this point. I get to this jsp from another jsp with a "dispatcher.forward(request, response);" statement. The full code can be viewed at: http://jus1170.tistory.com/16915
Thanks
 
Ivan Turner
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of casting to String I assigned it to a Object type. Now it works as expected and gives all attribute values. I know a String is an object but I didn't expect the object type to work as table output
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Turner wrote: I thought any item in an Enumeration could be cast to a string.


I just want to comment on this one statement. The truth is that nothing can be cast to a String except a String. More generally, a cast never changes the actual type of an object; it only clarifies to the compiler what the actual type of an object will be in the running program. So if a method like getAttribute() is declared to return the type java.lang.Object, but you know that the value of a particular attribute is always a java.lang.String, then you can make the cast for that one value of the attribute. Other times, for other arguments, that same method might actually (as here) return some other class, like DispatchTargetsInfo. You cannot cast a DispatchTargetsInfo to java.lang.String, because a DispatchTargetsInfo simply is not a java.lang.String.

Again, a cast never really changes anything; it just tells the compiler something you know but it does not. If you're wrong, or you lie, then there's going to be a ClassCastException at runtime.
 
Ivan Turner
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill - Thanks for the explanation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic