This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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 ?
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.
Joined: Jan 10, 2002
Thanks for your replies, Mike this is the code I use to instantiate the user User user = new User("firstname.lastname@example.org", "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 ]
Joined: Jan 10, 2002
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