java - BeanUtils throwing NullPointerException in hashCode() -


i using org.apache.commons.beanutils.beanutilsbean copy properties between objects. it's throwing nullpointerexception exception in hashcode() method. when check db, prodcode property not null. shouldn't throw nullpointerexception.

my dto object below. 1 notable thing is, store same equipmentdto child equipments equipment. equipment has same property. use recursive pattern.

eg. 1 parent equipment can have many child equipment.

public interface copiable {      public object copy(); }    public class equipmentdto implements copiable {     private long equipid;     private string prodcode;     private list<equipmentdto > childequipmentlist;  // recursively store child equipment      public equipmentdto () {        super();     }      public long getequipmentid() {        return equipid;     }      public void setequipmentid(long equipid) {        this.equipid= equipid;     }      public string getprodcode() {        return prodcode;     }       public int hashcode() {        return getprodcode().hashcode();     }          @override     public boolean equals(object o){        if(o == null)return false;         if(!(o instanceof equipmentdto ) ) return false;         equipmentdto other = (equipmentdto ) o;         return this.prodcode.equals(other.getprodcode());      }  } 

the nullpointerexception occurs in production environment. use java 1.6, oracle weblogic.

update

beanutils code snipt.

public static void copy(object src, object dest) {      try {         org.apache.commons.beanutils.beanutilsbean beanutil = beanutilsbean.getinstance();         beanutil.getconvertutils().register(false, true, 0);         beanutil.copyproperties(dest, src);     } catch (invocationtargetexception ite) {         //set dest null if exception         ite.printstacktrace();         dest = null;     } catch (illegalaccessexception iae) {         //set dest null if exception         iae.printstacktrace();         dest = null;     } } 

update

here stack trace.

caused by: java.lang.nullpointerexception     @ com.xxxx.xxxx.xxx.vo.equipmentdto.hashcode(equipmentdto.java:361)     @ java.lang.object.tostring(object.java:219)     @ java.lang.string.valueof(string.java:2826)     @ org.apache.commons.beanutils.beanutilsbean.copyproperties(beanutilsbean.java:246) 

you have exception, because copying empty dest bean has null value in prodcode field. way dest.hashcode() throws exception , consequence, dest.tostring() (which uses default implementation of object.tostring() calls hash code) throws nullpointerexception. tostring() method called beanutilsbean make debug output:

if (log.isdebugenabled()) {     log.debug("beanutils.copyproperties(" + dest + ", " +               orig + ")"); } 

so here dest converted string causing nullpointerexception.

in general in java it's presumed published objects tostring(), hashcode() , equals() never throw exception. it's better fix hashcode() implementation using:

string prodcode = getprodcode(); return prodcode == null ? 0 : prodcode.hashcode(); 

or if can use java-7:

return objects.hashcode(getprodcode()); 

Comments