aspose file tools*
The moose likes Beginning Java and the fly likes Object access within another Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object access within another Object" Watch "Object access within another Object" New topic
Author

Object access within another Object

Ed Wilkinson
Greenhorn

Joined: Jan 10, 2002
Posts: 7
Hello,
I have the following code:

When I do instaniate a user object and try to call getFirstName() for that object my result always comes back null. What am I doing wrong?
Thanks,
Ed
(edited by Cindy to format code)
[ March 04, 2002: Message edited by: Cindy Glass ]
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
Where is the code that you are using to instantiate the object and get the name?
Also , it looks like in the constructor, you are storing the Name as one field. Yet the method getName refers to other methods - getFirst, getMiddle and so on. Where are those methods defined ?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Shivaji... just look at the code. Those methods are on a Name object.

Ed.. from a design point, this code is a little bit dangerous. What if someone instantiates a User objects like so:This code will throw a NullPointerException, because getFirstName() refers to an object (this.name) that has not been intialized.

getFirstName() is also a poor choice for method name, since you are returning that person's full name. As a final point on 'style', all occurence of the 'this' keyword (in this particular class) are not necessary. There is no ambiguity between name and _name in your constructor, and in the getFirstName() method, the name object is part of the class in which the method is defined, so no 'this' is required.

As for the question at hand:
What is the exact output? Is it "null null null" or a single "null". Show us the code that is using the class, including where you instantiate the Name class to include with the User instantiation.
Ed Wilkinson
Greenhorn

Joined: Jan 10, 2002
Posts: 7
Thanks for your replies, Mike this is the code I use to instantiate the user
User user = new User("bqpublic@email.com", "password", new Name("first", "middle", "last"), new Address());
and I call the method like this:
user.getWholeName(); //changed the method name due to your suggestion, thanks
and I get an output of a single null.
Thanks
Ed
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Not sure what the problem is. Here is my code, and this works as expected...The output of running this program is:Note, the only reason I'm making the classes static is so that I can create them in the main method of the Driver class.
[ March 02, 2002: Message edited by: Mike Curwen ]
Ed Wilkinson
Greenhorn

Joined: Jan 10, 2002
Posts: 7
Thanks for your help Mike, I guess my problem is a little deeper. I'm actually using SOAP to instaniate the User object on the server side and pass the new object to the client, then based on information from the client I was trying to edit the Name object of the User on the server side and then pass the resulting new User object back to the client.
Ed
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object access within another Object