Home arrow static arrow Java Programming [Archive] - Stumped.. ClassCastExcpetion
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Stumped.. ClassCastExcpetion
2 Duke Stars available
This topic has 8 replies on 1 page.

Posts:85
Registered: 2/14/02
Stumped.. ClassCastExcpetion  
Jul 7, 2004 10:45 AM



 
I have a method that populates a vector from the db with an object TempRebateObj. Another class accesses this method and then is supposed to populate a JTable. The problem is that when I'm trying to get the items in the vector I get at ClassCastException. Here's my code... can anyone tell me why this is happening.

[code]
v = RebatesDB.searchRebates(str, type);

System.out.println("v size is " + v.size());
for(int i = 0; i < v.size(); i++)
{
System.out.println("The class is " + v.elementAt(i).getClass().getName());
tempRebateObj = (TempRebateObj)v.elementAt(i);
}
[\code]

This is my output.....

In my DAO I access it to see what it is.. and it's a TempRebateObj. That's what the first 2 lines are below.

The class of com.rebates.api.TempRebateObj@1077fc9 is com.rebates.api.TempRebateObj
The class of com.rebates.api.TempRebateObj@1568fb5 is com.rebates.api.TempRebateObj
v size is 79
The class is com.rebates.api.TempRebateObj
java.lang.ClassCastException
at com.rebates.SearchResults.<init>(SearchResults.java:38)
at com.rebates.SearchRebates.actionPerformed(SearchRebates.java:174)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1764)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1817)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:419)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
at java.awt.Component.processMouseEvent(Component.java:5134)
at java.awt.Component.processEvent(Component.java:4931)
at java.awt.Container.processEvent(Container.java:1566)
at java.awt.Component.dispatchEventImpl(Component.java:3639)
at java.awt.Container.dispatchEventImpl(Container.java:1623)
at java.awt.Component.dispatchEvent(Component.java:3480)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3450)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3165)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3095)
at java.awt.Container.dispatchEventImpl(Container.java:1609)
at java.awt.Window.dispatchEventImpl(Window.java:1590)
at java.awt.Component.dispatchEvent(Component.java:3480)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
is javax.swing.event.InternalFrameEvent[INTERNAL_FRAME_CLOSING] on Search Rebates
is javax.swing.event.InternalFrameEvent[INTERNAL_FRAME_DEACTIVATED] on Search Rebates
is javax.swing.event.InternalFrameEvent[INTERNAL_FRAME_CLOSED] on Search Rebates
Process Exit...
 

Posts:1,183
Registered: 1/23/02
Re: Stumped.. ClassCastExcpetion  
Jul 7, 2004 10:53 AM (reply 1 of 8)



 
My guess is that the RebatesDB.searchRebates() code path does some funky relfective classloading. Is that so?
 

Posts:10,972
Registered: 10/23/03
Re: Stumped.. ClassCastExcpetion  
Jul 7, 2004 10:53 AM (reply 2 of 8)



 
That's my guess too. Perhaps the class was loaded twice, by different class loaders.
 

Posts:85
Registered: 2/14/02
Re: Stumped.. ClassCastExcpetion  
Jul 7, 2004 11:25 AM (reply 3 of 8)



 
What's the best way I can try and trace this.
 

Posts:10,972
Registered: 10/23/03
Re: Stumped.. ClassCastExcpetion  
Jul 7, 2004 11:28 AM (reply 4 of 8)



 
To verify the class loaders are difference, you can use obj.getClass().getClassLoader().
 

Posts:85
Registered: 2/14/02
Re: Stumped.. ClassCastExcpetion  
Jul 7, 2004 11:35 AM (reply 5 of 8)



 
thanks... i tried it and it's the same class loader....

The class of com.rebates.api.TempRebateObj@bb494b is com.rebates.api.TempRebateObj

class loader is sun.misc.Launcher$AppClassLoader@12f6684

The class of com.rebates.api.TempRebateObj@1c8efd1 is com.rebates.api.TempRebateObj

class loader is sun.misc.Launcher$AppClassLoader@12f6684

v size is 79

The class is com.rebates.api.TempRebateObj

class loader is sun.misc.Launcher$AppClassLoader@12f6684

java.lang.ClassCastException
 

Posts:1,183
Registered: 1/23/02
Re: Stumped.. ClassCastExcpetion  
Jul 7, 2004 11:41 AM (reply 6 of 8)



 
OK, so classloaders are not it. Silly question, but is TempRebateObj imported correctly (i.e. is TempRebateObj definitely the same as com.rebates.api.TempRebateObj in your code snippet)?
 

Posts:1,183
Registered: 1/23/02
Re: Stumped.. ClassCastExcpetion  
Jul 7, 2004 11:49 AM (reply 7 of 8)



 
OK, so classloaders are not it.

Although to be sure there's no funkiness on the other side, could you add

System.out.println(TempRebateObj.class + " loaded by " + TempRebateObj.class.getClassLoader());

immediately above

tempRebateObj = (TempRebateObj)v.elementAt(i);

?
 

Posts:85
Registered: 2/14/02
Re: Stumped.. ClassCastExcpetion  
Jul 7, 2004 11:52 AM (reply 8 of 8)



 
That must have been it... i specifically created the object as com.rebates.api.TempRebatesObj and it worked. I don't know why but it looked like it was accessing it correctly.

Thanks. for the help.
 
This topic has 8 replies on 1 page.