Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

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

 
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:




 
author & internet detective
Posts: 39206
726
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
Posts: 39206
726
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
 
Bartender
Posts: 10759
68
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
 
What a show! What atmosphere! What fun! What a tiny ad!
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!