• 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

Calling an object from another class and printing out result - any help?

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have written a class called Title, which contains information about films, including the title, lead actors, director etc. This information is stored in a text file, which is then called from the class Title.

To save me from having to rewrite this information in the second class, called Video, I want to be able to call the information in the Title class in the toString method within the Video class and print it out along with the additional information contained within the Video class.

The problem is that at the moment, the toString method within the Video class is only returning 'null' in place of the information from the Title class.

Any ideas or help would be appreciated.

My code so far is as follows,

1. The Video Class,



2. The Title class,



Also, the toString method in the Title class works fine by itself, I'm just having a problem trying to call this information from the Video class.

Any ideas how to achieve this?
 
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
Your Video class has two constructors. If you use the first one, and pass in a Title object, then you shouldn't see this null; but if you use the second one, then the "title" member is null because it's never assigned anywhere, and you will see "null" in toString().

Each of your constructors needs to fully initialize the class.
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think I understand what you mean in this case.

However, if the information for the Title class is contained in a text file, which is currently called from the tester main class using the following code,



I don't understand how I can call the Title information in the Video class, and still get it to print it all out in the current tester main class.

I must be missing something here. If I can call this information in the Title class, I don't understand why this same information can't be added to the Video class as well, and then be printed out using a main class.

Any help or advice!!
 
Ernest Friedman-Hill
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
In the last bit of code you showed me, you created a Title object and a Video object, but the Video object doesn't "know" about that Title object. You could just slightly modify the Video constructor:



Then create the Video like this:



Now, there are other ways to do it, and I wouldn't call this the best way -- far from it, actually, as this arrangement with multiple objects reading their data from a single file, depending on being constructed in a certain order, is pretty nasty. A somewhat better thing to do might actually be



because at least there is less potential for error. But that's up to you.
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thank you so much for your help. I see exactly what you mean now.

Another Java puzzle solved at last.

Have a great weekend.
 
reply
    Bookmark Topic Watch Topic
  • New Topic