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]
# 1

> 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.

CeciNEstPasUnProgrammeura at 2007-7-29 15:52:42 > top of Java-index,Java Essentials,Java Programming...
# 2

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

kanthua at 2007-7-29 15:52:43 > top of Java-index,Java Essentials,Java Programming...
# 3

I don't see an exception.

By the way, please read this:

http://forum.java.sun.com/help.jspa?sec=formatting

CeciNEstPasUnProgrammeura at 2007-7-29 15:52:43 > top of Java-index,Java Essentials,Java Programming...
# 4

> SoapRequest=new Object();

> SoapRequest= arg_req;

WTF is that?

CeciNEstPasUnProgrammeura at 2007-7-29 15:52:43 > top of Java-index,Java Essentials,Java Programming...
# 5

Holy moses...that code.

throws ClassCastException!? O_o

Maybe the class is loaded with a different classloader.

-Kayaman-a at 2007-7-29 15:52:43 > top of Java-index,Java Essentials,Java Programming...
# 6

> > SoapRequest=new Object();

> > SoapRequest= arg_req;

>

> WTF is that?

Something that won't compile :-)

georgemca at 2007-7-29 15:52:43 > top of Java-index,Java Essentials,Java Programming...
# 7

Anyway, obviously MethodSoapRequestDS is not a SoapRequestDS, according to instanceof.

CeciNEstPasUnProgrammeura at 2007-7-29 15:52:43 > top of Java-index,Java Essentials,Java Programming...