• 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

ArrayList Problem

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a problem with the code below:-



The public string toString() method doesn't work and produces a class cast exception: null, Ithink this is because the arrayList is empty but I can't figure out why it is empty.

Thanks in advance to anyone who can shed some light onto this problem.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't think you're problem is with the ArrayList not being populated .. the hint is in the exception ClassCastException. This means that the object you are trying to cast to is not of that type.
When you call your addOrders method you pass it an object of type item and add that to the ArrayList but when you call toString or calcOrderCost you cast the items you take off the ArrayList to Order.
You might also want to have a look at the Collections tutorial on java.sun.com it's considered better practice to iterate through your ArrayList using the Collection methods than the way you have.
Anyway hope that helps..
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic