Object access within another Object

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 ]

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 ?

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

posted Mar 02, 2002 17:06:00

0

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

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

posted Mar 03, 2002 00:07:00

0

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