wood burning stoves*
The moose likes JSP and the fly likes cannot be cast to String error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "cannot be cast to String error" Watch "cannot be cast to String error" New topic
Author

cannot be cast to String error

Ivan Turner
Ranch Hand

Joined: Feb 27, 2012
Posts: 37
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

Joined: Feb 27, 2012
Posts: 37
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Feb 27, 2012
Posts: 37
Ernest Friedman-Hill - Thanks for the explanation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cannot be cast to String error
 
Similar Threads
List values are not bind with text fields
Number Format Exception
Error comes whilke compiling the jsp page
exception
Sometimes users see other's secure data over ssl on jsp struts and glassfish