• 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

cant retrieve data.

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???)
 
ken zemaitis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic