Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting error while using for-each loop

 
Gopi Kiran Manusani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please tell me whats wrong in my code



Here productNumbersList and totalProductsList filled with data coming from db.

I am getting "java.lang.ClassCastException:" Exception at boleded line or line number 5

Please suggest where it goes wrong..
 
Vidmantas Maskoliunas
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

it must be an issue with "legacy" code which added different kind of object than ProductSearchVO into your totalProductsList. Try to get the real type of objects by following this idea:


After executing the code you should know if the product list is filled with the right type of products and without nulls.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you put an object of a different type into a List that is typed via generics?
 
Vidmantas Maskoliunas
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock wrote:How do you put an object of a different type into a List that is typed via generics?


It's possible via "legacy" (not-generics'ized) code, as generics are type-safe only at compilation time but not runtime:
Something like


In the case above, if the values are fetched from DB as author said, it could be the code that fills the list with values from DB.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, that would do it. I skimmed his code and assumed this represented the problem - didn't realise the problem is actually to do with some other code he has not posted.
 
Gopi Kiran Manusani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
sorry, it was my mistake, in my lengthy code the totalProductsList list is mistakenly raplaced with some other list,It is working now..Thanks a lot for your response
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic