Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java.lang.ClassCastException

Reply
Thread Tools

java.lang.ClassCastException

 
 
Paolo
Guest
Posts: n/a
 
      04-01-2006
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


 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      04-01-2006

"Paolo" <(E-Mail Removed)> wrote in message
news:G2uXf.44264$(E-Mail Removed).. .
> 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() .


 
Reply With Quote
 
 
 
 
Paolo
Guest
Posts: n/a
 
      04-01-2006
> 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


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-01-2006
On Sat, 1 Apr 2006 14:08:07 +0200, "Paolo"
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      04-01-2006
Paolo wrote:
>>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
>
>


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
 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      04-01-2006
Paolo wrote:
> 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.

--
Tony Morris
http://tmorris.net/

s/Commonwealth Games/Commonwealth Swimming
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      04-02-2006

"Paolo" <(E-Mail Removed)> wrote in message
news:7tzXf.44786$(E-Mail Removed).. .
>> 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.


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.


 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      04-02-2006

"Patricia Shanahan" <(E-Mail Removed)> wrote in message
newsLzXf.11013$(E-Mail Removed) hlink.net...
> Paolo wrote:
>>>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
>>
>>

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


 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      04-02-2006

"Tony Morris" <(E-Mail Removed)> wrote in message
news:442ef1df$(E-Mail Removed)...
>
> 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.


 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      04-02-2006
Mike Schilling wrote:
> "Tony Morris" <(E-Mail Removed)> wrote in message
> news:442ef1df$(E-Mail Removed)...
>> 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.
>
>


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

--
Tony Morris
http://tmorris.net/

s/Commonwealth Games/Commonwealth Swimming
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments