• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ClassCastException in Tag Library

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an object that is implemented, fundamentally, as an ArrayList:



The method as shown above, getItems() is what I'm trying to do.

In another class (this is a Tag Library, if that makes a difference), I have the following attempt to access this list as an array:



All the following combinations compile fine, but throw a ClassCast Exception when tested:

Combo 1:



Combo 2:



And, it always references the line, regardless of which class it is in, where I attempt to do the cast from Object to menuitem[].

However, interestingly enough (in my opinion) this does work:



Obviously, the difference is that no cast is required in the last (working) example.

Can someone tell me what's going on here? Why doesn't the cast work? And is there any way to do this with ArrayList's built-in toArray function (which I believe requires a cast in any case)?

TIA
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since this is more of a fundamental Java question than a taglib specific one, I'm going to move this to Java In General (Internmediate).
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Obviously, the difference is that no cast is required in the last (working) example.

Can someone tell me what's going on here? Why doesn't the cast work? And is there any way to do this with ArrayList's built-in toArray function (which I believe requires a cast in any case)?



There are two overloaded versions of the Collection.toArray() method. The default one (no parameters) return everything in an Object array. An object array can hold any type of objects. However, just because all the items in the array are menu item objects, doesn't make the array a menu item array -- it is still an object array.

The other overloaded method is what you need. It takes an array that it will either use directly, or create another by type, and return an object array that IS-A type of array that you want. Basically....



Henry
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Henry! I did what you said, and it worked as advertised. I still don't understand, though, why the other way couldn't be cast. Let me ask this:

Even though menuitem[] miarray= (menuitem[]) items.toArray() doesn't work, will this:

Object oa[]= items.toArray();
menuitem mi= (menuitem) oa[0]

If so, what is the subtle distinction I'm missing here? I guess this is academic at this point, since what you gave fixed my problem, but it is curious.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed. It is a subtle distinction. You just have to remember that an array is simply an object that holds objects -- it's type doesn't change based on what it is holding.

In fact, let me modify your third example, to return the equivalent of what the toArray() method is returning.



The items in the array IS-A menu item object, and hence, can be casted. However, the array is still an Object array, and hence, can't be casted.

Henry
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gotcha! Thanks for the patience, and the education!
 
reply
    Bookmark Topic Watch Topic
  • New Topic