This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes ClassCastException when attempting to cast an array.  Component.getContents() blamed. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ClassCastException when attempting to cast an array.  Component.getContents() blamed." Watch "ClassCastException when attempting to cast an array.  Component.getContents() blamed." New topic
Author

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

Carlos Failde
Ranch Hand

Joined: Oct 20, 2000
Posts: 84

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 ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
KR Campbell
Ranch Hand

Joined: Mar 26, 2004
Posts: 124
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

Joined: Oct 20, 2000
Posts: 84
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

Joined: Dec 10, 2001
Posts: 7023
Carlos,
I think you get it.
Carlos Failde
Ranch Hand

Joined: Oct 20, 2000
Posts: 84
Great. Thanks.
 
Don't get me started about those stupid light bulbs.
 
subject: ClassCastException when attempting to cast an array. Component.getContents() blamed.
 
Similar Threads
Arrow navigation not working on JComboBox with null element
container's containment
How to work with a ScrollPane in a SplitPane?
Can't get the desired position of label
Component Order