java.lang.ClassCastException

Discussion in 'Java' started by Paolo, Apr 1, 2006.

  1. Paolo

    Paolo Guest

    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
    Paolo, Apr 1, 2006
    #1
    1. Advertising

  2. "Paolo" <> wrote in message
    news:G2uXf.44264$...
    > 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() .
    Mike Schilling, Apr 1, 2006
    #2
    1. Advertising

  3. Paolo

    Paolo Guest

    > 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
    Paolo, Apr 1, 2006
    #3
  4. Paolo

    Roedy Green Guest

    On Sat, 1 Apr 2006 14:08:07 +0200, "Paolo"
    <> 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.
    Roedy Green, Apr 1, 2006
    #4
  5. 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
    Patricia Shanahan, Apr 1, 2006
    #5
  6. Paolo

    Tony Morris Guest

    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
    Tony Morris, Apr 1, 2006
    #6
  7. "Paolo" <> wrote in message
    news:7tzXf.44786$...
    >> 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.
    Mike Schilling, Apr 2, 2006
    #7
  8. "Patricia Shanahan" <> wrote in message
    news:pLzXf.11013$...
    > 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.
    Mike Schilling, Apr 2, 2006
    #8
  9. "Tony Morris" <> wrote in message
    news:442ef1df$...
    >
    > 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.
    Mike Schilling, Apr 2, 2006
    #9
  10. Paolo

    Tony Morris Guest

    Mike Schilling wrote:
    > "Tony Morris" <> wrote in message
    > news:442ef1df$...
    >> 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
    Tony Morris, Apr 2, 2006
    #10
  11. "Tony Morris" <> wrote in message
    news:44304445$...
    > Mike Schilling wrote:
    >> "Tony Morris" <> wrote in message
    >> news:442ef1df$...
    >>> 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.


    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 ? :)
    Mike Schilling, Apr 2, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Dubery
    Replies:
    5
    Views:
    575
    Bob Dubery
    Jun 19, 2004
  2. mgalaty
    Replies:
    0
    Views:
    2,712
    mgalaty
    Sep 10, 2004
  3. patrick
    Replies:
    0
    Views:
    3,405
    patrick
    Aug 20, 2005
  4. Replies:
    1
    Views:
    9,639
    Bjorn Abelli
    May 1, 2006
  5. marco
    Replies:
    6
    Views:
    22,040
    qcarver
    May 18, 2007
Loading...

Share This Page