• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassCastException in Tag Library

 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • 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
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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).
 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Gotcha! Thanks for the patience, and the education!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic