• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

ClassCastException when attempting to cast an array. Component.getContents() blamed.

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gives a ClassCastException at runtime -
Exception in thread "main" java.lang.ClassCastException: [Ljava.awt.Component;
The return type of the getComponents() method in the Component class is Component[]. The attempted cast seems valid since all the components returned are in fact instances of JLabel as can be proved by calling getCLass() on each of the returned components. The ClassCastException is claiming that the components are instances of Component.
Changing the troublesome line to

corrects the problem.
Another possible change is to replace the last three lines of the program with

which also gives the desired result.

Trying something a bit different:

I.E. record the components added to the panel so that the getContents() call is no longer needed. The JLabels are stored in an array of Component references which is later casted to an array of JLabel.
It works so it can not be a casting problem, it must be something peculiar about getContents() !?!?
[ April 22, 2004: Message edited by: Carlos Failde ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The concept to be clear about here is that an object of type Component[] is not necessarily also an object of type JPanel[].
In your particular case, the array returned by calling JPanel.getComponents() is an array of type Component[]. Even if every component of the returned array is a JPanel, that still doesn't make the returned array object an array of type JPanel[].
So, the only particular about calling getComponents, is that you get a reference to an array of type Component[].
Clear?
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a look at this yesterday and it puzzled me as well.
Although it would make no difference, he's actually looking for a JLabel[]. I follow what you are saying, but what is throwing me is that JLabel inherits from Component. So it would seem like a JLabel[] would conform to a Component[].
I looked through the JLSpec. and from what I found it seemed to suggest that if an object SC type conforms to another object TC then a cast of SC[] to TC[] was legitimate. Clearly it is not in this case, but why not? Is there something else in the language specification that I missed?
Regards,
Kenny
 
Carlos Failde
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh, an array object is an object! I had thought of array objects until now as a bunch of references to objects (umm, "an array of objects" I guess). But seeing as they're objects in their own right they obey the usual rules of objects, such as having a type.
Spelling things out we have Identifiers, array objects, and objects.

1) Identifiers
Component[] comps is a reference to an object of type Component[]. It can refer to an object of type Component[] or one of its subclasses such as an object of type JLabel[] (a JLabel[] is a Component[]).
JLabel[] labels is a reference to an object of type JLabel[]. It can refer to an object of type JLabel[].
2) array objects
new JLabel[4]; constructs an object of type JLabel[]. It can store references to objects of type JLabel.
new Component[4]; constructs an object of type Component[]. It can store references to objects of type Component or a subclass of Component such as JLabel.
The object constructed by new JLabel[4]; can be referred to by comps because JLabel[] is a Component[]. Additionally the object constructed by new Component[4]; or returned by getComponents() (which has return type Component[]) in Container can be referred to by comps.
The object constructed by new JLabel[4]; can be referred to by labels BUT the object constructed by new Component[4]; or returned by getComponents() can NOT be referred to by labels because Component[] is not a JLabel[].

3) Objects
new JLabel( "label " ); constructs an object of type JLabel. A reference to this object can be stored in the object constructed with new JLabel[4]; OR in the object constructed with new Component[4]; because a JLabel is a Component.
new Component(); constructs an object of type Component. A reference to this object can be stored in the object constructed with new Component[4]; BUT not in the object constructed with new JLabel[4]; because a Component is not a JLabel.
[ April 23, 2004: Message edited by: Carlos Failde ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carlos,
I think you get it.
 
Carlos Failde
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great. Thanks.
reply
    Bookmark Topic Watch Topic
  • New Topic