• 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

Object access within another Object

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic