java.lang.ClassCastException

P

Paolo

Hi all,
inside one of my Class i've method which make this operation:
......
Call call = (Call) new Service().createCall();
call.setTargetEndpointAddress(new
URL("http://localhost:8080/axis/services/"));
call.setOperationName(new QName("urn:SalutoWS", "saluto"));
Object rispostaWS = call.invoke(new Object[]{"sum"});
array = (ArrayList)rispostaWS;
........

When is doing this, it's return the follow exeption:
java.lang.ClassCastException
At the line (array = (ArrayList)rispostaWS;).

How can be possible that the line can't make the cast between an Object and
ArrayList?

Thank's all.

Paolo
 
M

Mike Schilling

Paolo said:
Hi all,
inside one of my Class i've method which make this operation:
.....
Call call = (Call) new Service().createCall();
call.setTargetEndpointAddress(new
URL("http://localhost:8080/axis/services/"));
call.setOperationName(new QName("urn:SalutoWS", "saluto"));
Object rispostaWS = call.invoke(new Object[]{"sum"});
array = (ArrayList)rispostaWS;
.......

When is doing this, it's return the follow exeption:
java.lang.ClassCastException
At the line (array = (ArrayList)rispostaWS;).

How can be possible that the line can't make the cast between an Object
and ArrayList?

rispostaWS must not be an ArrayList. What type is it? If you're not sure,
print out rispostaWS.getClass() .
 
P

Paolo

rispostaWS must not be an ArrayList. What type is it? If you're not
sure, print out rispostaWS.getClass() .

rispostaWS is an java.lang.Object type, but seem that it can't make a cast.
Any ideas.
Thanks
Paolo
 
R

Roedy Green

array = (ArrayList)rispostaWS;

The key to this conundrum is the declaration for array.

if it were:

ArrayList array;

then the code should compile, but fail at run time if repostAWS were
not really an ArrayList.
 
P

Patricia Shanahan

Paolo said:
rispostaWS is an java.lang.Object type, but seem that it can't make a cast.
Any ideas.
Thanks
Paolo

You need to find out how you are supposed to use your respostaWS. It
should be explained in the documentation for the method that returned it.

If the class of the object is java.lang.Object, it is not an ArrayList
and can never turn into one. The only objects that can be treated as
ArrayList are ArrayList objects, and objects of classes that extend
ArrayList, such as the subclasses AttributeList, RoleList, and
RoleUnresolvedList.

Of course, an ArrayList object can be referenced by a variable of type
Object, but getClass gets the actual class of the object, not just the
type of the reference. If it says "java.lang.Object" you really do have
an object of class Object.

Patricia
 
T

Tony Morris

Paolo said:
Hi all,
inside one of my Class i've method which make this operation:
.....
Call call = (Call) new Service().createCall();
call.setTargetEndpointAddress(new
URL("http://localhost:8080/axis/services/"));
call.setOperationName(new QName("urn:SalutoWS", "saluto"));
Object rispostaWS = call.invoke(new Object[]{"sum"});
array = (ArrayList)rispostaWS;
.......

When is doing this, it's return the follow exeption:
java.lang.ClassCastException
At the line (array = (ArrayList)rispostaWS;).

How can be possible that the line can't make the cast between an Object and
ArrayList?

Thank's all.

Paolo

That "array is not an ArrayList" is a red herring and blindly believing
it has led to many hours of frustration for lots of people, so I urge
you to ignore it for now. There is a possible use case where array is
indeed an ArrayList instance type and the cast will fail as you are
observing, but putting that aside, it is often easier to rule out some
easier cases first - for example, the one where array is actually not an
ArrayList.

You can achieve this with a debugger, or more simply:
System.out.println(array instanceof ArrayList).

If you observe the output to be true and you also observe a failed cast,
you must now explore which class loader is being used under different
contexts to determine which class (java.lang.Class) is being loaded
under a class loader that you do not expect. The Class.isInstance method
is handy for this, but I prefer to use a debugger. Worry about that if
you get to it.
 
M

Mike Schilling

Paolo said:
rispostaWS is an java.lang.Object type, but seem that it can't make a
cast.

No, the *reference* is of type Object; the question is what the actual type
of the object it points to is. If you do the print I suggested, you'll have
the answer.
 
M

Mike Schilling

Patricia Shanahan said:
You need to find out how you are supposed to use your respostaWS. It
should be explained in the documentation for the method that returned it.

Not in this case. Call.invoke() is a generic [1] method for invoking web
services. To determine the type returned in this case would require
checking the WSDL to see what schema type the operation returns and then
discoving how that type is mapped into Java.

1. Not in the 1.5 sense of generic.
 
M

Mike Schilling

Tony Morris said:
If you observe the output to be true and you also observe a failed cast,
you must now explore which class loader is being used under different
contexts to determine which class (java.lang.Class) is being loaded under
a class loader that you do not expect. The Class.isInstance method is
handy for this, but I prefer to use a debugger. Worry about that if you
get to it.

This is very unlikely to occur with system (java.lang.XXX) classes, given
that any well-behaved class loader will defer to the system class loader to
load them. I'm not sure it can occur with system classes at all, but I'm
not enough of an expert to be certain it can't.

At any rate, in this instance it's at least a 99% chance that the object
simply isn't an ArrayList.
 
T

Tony Morris

Mike said:
This is very unlikely to occur with system (java.lang.XXX) classes, given
that any well-behaved class loader will defer to the system class loader to
load them. I'm not sure it can occur with system classes at all, but I'm
not enough of an expert to be certain it can't.

At any rate, in this instance it's at least a 99% chance that the object
simply isn't an ArrayList.

I'm going to bet that indeed it is an ArrayList, since I expect that the
provided call includes a remote call - and therefore, class loaders
doing what they do to permit classes to load over the wire.

Pure speculation, but my money is on a class loader problem
(configuration?) :) Time will tell (or maybe not?).
 
M

Mike Schilling

Tony Morris said:
I'm going to bet that indeed it is an ArrayList, since I expect that the
provided call includes a remote call - and therefore, class loaders doing
what they do to permit classes to load over the wire.

Not java system classes. And not an XML web service, which is sure what the
code looks like.
Pure speculation, but my money is on a class loader problem
(configuration?) :)

How much ? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top