• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException  RSS feed

 
sujesh Katri
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.



 
Matthew Brown
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.
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • 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
 
Joanne Neal
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!