wood burning stoves 2.0*
The moose likes Java in General and the fly likes Calling an object from another class and printing out result - any help? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calling an object from another class and printing out result - any help?" Watch "Calling an object from another class and printing out result - any help?" New topic
Author

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

celine scarlett
Ranch Hand

Joined: Nov 06, 2005
Posts: 93
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?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24185
    
  34

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.


[Jess in Action][AskingGoodQuestions]
celine scarlett
Ranch Hand

Joined: Nov 06, 2005
Posts: 93
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
Marshal

Joined: Jul 08, 2003
Posts: 24185
    
  34

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

Joined: Nov 06, 2005
Posts: 93
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calling an object from another class and printing out result - any help?