Bookmark Topic Watch Topic
  • New Topic

Composition  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi guys long time since I posted on the forum but anyway I'm getting back in to it my question is how come this code is not valid,I want to be able to call the sayHello() function in the main method through the birthday object

so p.getOne.sayHello();

but when I do this I get an Exception in thread "main" java.lang.NullPointerException
at VehicleMain.main(VehicleMain.java:8)

How come I get this error,and how would I go about fixing this problem?


MAIN


Person.java




Birthday.java

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Your Person class never initialize a Birthday instance.... So, your getOne() method returns null, and when you try to use null to call the sayHello() method, you will get a NPE.

Henry
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Report post to moderator
Henry Wong wrote:Your Person class never initialize a Birthday instance.... So, your getOne() method returns null, and when you try to use null to call the sayHello() method, you will get a NPE.

Henry


Hi Henry that works now when I initialised the object  but just wondering how these actually works,

Thanks =)
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
Adam Chalkley wrote:that works now when I initialised the object  but just wondering how these actually works,

Could you give more details to you question?  How what actually works?
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
It looks like this topic is continued here:

https://coderanch.com/t/677786/java/Composition-part

Locking this topic.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!