Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException  RSS feed

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm getting NullPointerException while i'm accessing my equals method..i'm placing my code below ...some one please tell me.



 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do you set c1.id? Because you're calling equals() on that variable.
 
Marshal
Posts: 58366
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because your id in the c1 instance is still null.
you should write a constructor to set the id and throw an Exception if null is passed. Similarly in the setId method. You asked about it in another thread.

If you have to permit nulls, use this method instead of calling equals directly. You can also use this method to prevent nulls entering your class.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i set value to c1...c1=c is it correct
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sujesh Katri wrote:how can i set value to c1...c1=c is it correct


No. You've already set c1. As Campbell and Matthew said, it's the id in the object c1 refers to that has not been set.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!