File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes cant retrieve data. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cant retrieve data." Watch "cant retrieve data." New topic
Author

cant retrieve data.

ken zemaitis
Ranch Hand

Joined: Sep 26, 2005
Posts: 42
Hi everyone. I'm getting the same error while compiling a few of my progams. What is wrong with this: " System.out.println(fruits[i].info());"?




Thanks.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

When you put (references to) your Fruit objects into an Object array, the reference type is upcast to Object. So when you get it back out, you need to explicitly downcast it back to Fruit.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10908
    
  12

It would help in the future if you posted the exact error message...

but, i think what the problem is is that you have an array of Objects. your reference is of type Object, not of type Fruit. so, there is no such thing as Object.info().

in other words, since fruits is declared as an Object reference, you can only call Object methods.

if you make it

Fruit[] fruits = new Fruits[0];

you'll find a brand new problem to fix!!! (how big is the array you're declaring???)


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
ken zemaitis
Ranch Hand

Joined: Sep 26, 2005
Posts: 42
Originally posted by fred rosenberger:
It would help in the future if you posted the exact error message...

but, i think what the problem is is that you have an array of Objects. your reference is of type Object, not of type Fruit. so, there is no such thing as Object.info().

in other words, since fruits is declared as an Object reference, you can only call Object methods.

if you make it

Fruit[] fruits = new Fruits[0];

you'll find a brand new problem to fix!!! (how big is the array you're declaring???)


Next time I will definitely post the exact error message. Thank you for taking the time to look at my problem and thank you for HELPING me figure it out. Something just clicked...
ken zemaitis
Ranch Hand

Joined: Sep 26, 2005
Posts: 42
Originally posted by marc weber:
When you put (references to) your Fruit objects into an Object array, the reference type is upcast to Object. So when you get it back out, you need to explicitly downcast it back to Fruit.


Thank you again sir. I appreciate your wisdom.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cant retrieve data.
 
Similar Threads
Comparator giving compiler error
i am getting identifier expected error in line 4... can any one help me out
Polymorphic calls
The 'DateFormat' abstract class
please clear me this...