• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do i call a method that calls a method?  RSS feed

 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Person class, a Name class, and a demo. What i need is to call a method from the Person class that calls a method from the Name class for the demo. Heres an example:




 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel,
Consider changing

to


Java starts variables with lower case letters. Which makes it clearer whether you are using the class Name or the variable name.

You are getting the NullPointer because Person never instantiates name. You want to create a setter in the Person class like public void setName(Name n) { name = n): }. Then your tester class can pass in a Name object.
 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yeah, i didnt include that in my code example, oops. While i do have a set method, im trying a couple ways shown below, but still get a nullpointer. What else am i missing?



 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've succeeded in moving the NullPointer to your set method. But Person still doesn't instantiate Name. So it is null when you try to update it in the setter. You need to call new Name() in the Person constructor.
 
Daniel Hoang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think im allowed to edit the demo
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Hoang wrote:Heres an example:

Daniel,

Please DontWriteLongLines. It makes your threads very hard to read. I've broken your up this time.
Remember the rule: 80 charcters max.

Thanks

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!