• 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

Call methods of objects stored in a vector !

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello all,

it's been a while since i posted last, and i would like to thank any reading or answering this post. as i know your time is valuable.

i currently having problems with one specific part of my appliction. i have a vector which is storing objects of a class i have created (OrderItem). i can add/remove objects into/from the vector fine. i can use the Enumeration interface to go though the items and using a while loop and call the toString method of each object in the vector. but i would like to call a method within the object called saveOrderItem(). when i try to complie the code i get "can't resolve symbol saveOrderItem() from location class java.lang.Object" i don't understand, whats going on. i made OrderItem extend Object (a sujjestion)and still nothing(altough i didn't think it would). please help my coursework depends onit.

public void saveOrder() {

if (orderVector.isEmpty())
{
System.out.println("You Have Not Ordered Anything.");
}
else
{
Enumeration items = orderVector.elements();
while (items.hasMoreElements())
{
items.nextElement().saveOrderItem();

// saveOrderItem() is method of OrderItem
// many OrderItems in vector.
}
}

}

if you need to see any other code please say....


thanks

Tom R
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
complete guess, but maybe if you cast it.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoever told you to make OrderItem extend Object apparently isn't aware that any class that doesn't explicitly extend another class implicitly extends Object, so adding "extends Object" accomplishes exactly nothing.

The correct answer: the compiler doesn't know that the objects in the Vector are OrderItems. You have to tell it by using a "typecast"; it will then let you treat the object as an OrderItem. A typecast (or "cast" for short) is simply the name of the real class of the object in parentheses:

OrderItem item = (OrderItem) items.nextElement();
item.saveOrderItem();

You can do this all in one step by using some extra parentheses:

((OrderItem) items.nextElement()).saveOrderItem();
 
Tom Roffe
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, worked a treat. I did think that extending object would be a dead end as you said for that reason. But the person that (mis-)advised was adamant that it was needed.

Thanks again

Tom R
 
reply
    Bookmark Topic Watch Topic
  • New Topic