adding value objects to set problem

I am not able to add value objects to a set. There is error message with hashcode. I do overwrite the hashcode function like this:public int hashCode() {return getId().hashCode()^31;}
[211 byte] By [mm68a] at [2007-11-26 17:08:30]
# 1
What is the error?Why aren't you just returning the id's hashcode?
zadoka at 2007-7-8 23:36:21 > top of Java-index,Java Essentials,Java Programming...
# 2
what hashcode should I return though?
mm68a at 2007-7-8 23:36:21 > top of Java-index,Java Essentials,Java Programming...
# 3
I tried just return getId().hashcode(). Same error:hashCode(TransferVO.java:139)at java.util.HashMap.hash(HashMap.java:263)at java.util.HashMap.put(HashMap.java:378)at java.util.HashSet.add(HashSet.java:192)
mm68a at 2007-7-8 23:36:21 > top of Java-index,Java Essentials,Java Programming...
# 4
What is the error? NullPointerException? Is getId returning null?
doremifasollatidoa at 2007-7-8 23:36:21 > top of Java-index,Java Essentials,Java Programming...
# 5
yeah.
mm68a at 2007-7-8 23:36:21 > top of Java-index,Java Essentials,Java Programming...
# 6
> yeah.Well, you can't call a method on null. That is why you are getting the NullPointerException.
zadoka at 2007-7-8 23:36:21 > top of Java-index,Java Essentials,Java Programming...
# 7
i hope you are also aware that ^ is xor not raise to power.
mkoryaka at 2007-7-8 23:36:21 > top of Java-index,Java Essentials,Java Programming...
# 8

You can't invoke hashCode() on getId() when it is null.

change the hashCode() method implementation like this:

public int hashCode() {

Object o = getId();

if ( o == null) {

return -1;

else

return o.hashCode();

}

Message was edited by:

I_LOVE_JAVA

I_LOVE_JAVAa at 2007-7-8 23:36:21 > top of Java-index,Java Essentials,Java Programming...