• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main" java.lang.NullPointerException

 
Deepak Vishwakarma
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Added code tags - see UseCodeTags for details]

i am not able to find the exception why occur
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception message will tell you on exactly which line it occurs. That will help you work it out, but if it's not enough you should tell us which line, as it makes it much easier for us to help you.

However, I think I see the problem. Look at line 13. Was dob supposed to be a parameter passed in to the constructor? Because at the moment you're trying to initialise the instance variable dob with itself. So the reference you're trying to call getTime() on is null.
 
Ankit Chaudhary
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deepak,


Date is not passed as parameter to the constructor,this is causing NPE .



I think here you declared the myDate but its not passed as parameter.You can make the change as below.


Let me know if this is helpful.


Thanks and Best Regards,
Ankit
-------------------------------------------------------------------------------- --------------------------
AnkitChaudhary | ankchaudhary@infocepts.com | www.infocepts.com
--------------------------------------------------------------
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Date myDate = new Date();
BetterPerson myPerson = new BetterPerson("Deepak","Vishwakarna",myDate );
System.out.println(myPerson.getDOB());



to use this he needs to have 3-arg constructor also that takes the third parameter of the type Date.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic