Problem with ClassCastException
I have a typical problem...I have a method which accepts Object as a parameter and I would like to convert that into an object of class (SoapRequestDS). Calling tostring on the argument correctly shows that the object is of type SoapRequestDS. But when I try to cast it to SoapRequestDS, it throws up ClassCastException.
The SoapRequestDS class is user defined class.
The Object is coming from a vxml page which uses Object tag to call the java application. The vxml project references the jar file.
[518 byte] By [
kanthua] at [2007-11-27 11:23:40]

> I have a typical problem...I have a method which
> accepts Object as a parameter and I would like to
> convert that into an object of class (SoapRequestDS).
You are aware that casting only changes the type of the reference, not of the instance?
> Calling tostring on the argument correctly shows that
> the object is of type SoapRequestDS. But when I try
> to cast it to SoapRequestDS, it throws up
> ClassCastException.
Then it's not of type SoapRequestDS. Please show the code that prints the type and does the cast, and show the complete output.
I am posting the code and the output....
Code:
public void setRequest(Object arg_req) throws ClassCastException
{
try
{
System.out.println("Correct Method" + arg_req.getClass().getName());
System.out.println("Correct Method" + arg_req.toString());
Method[] methods=arg_req.getClass().getMethods();
System.out.println(methods[0].getName());
//SoapRequest=new SoapRequestDS();
SoapRequest=new Object();
SoapRequest= arg_req;
System.out.println(SoapRequest.getClass().getName());
if (arg_req instanceof SoapRequestDS) {
System.out.println("Correct");
//type new_name = (type) name;
}
System.out.println("Not Correct");
//SoapRequest=(SoapRequestDS)arg_req;
}
catch(ClassCastException e)
{
System.out.println("Class cast exception" + e);
}
}
Output
Correct MethodSoapRequestDS
Correct MethodSoapRequestDS@4a5435a2
getRequestDS
SoapRequestDS
Not Correct
SoapRequestDS
I don't see an exception.
By the way, please read this:
http://forum.java.sun.com/help.jspa?sec=formatting
> > SoapRequest=new Object();
> > SoapRequest= arg_req;
>
> WTF is that?
Something that won't compile :-)