java - Null Pointer Exception in setter of entity in hibernate -


i trying map one-to-one relation between user , proteindata entity getting null pointer exception in setter in user entity.

my main service code program.java

public class program {      public static void main(string a[]){           session session=hibernateutility.getsessionfactory().opensession();          session.begintransaction();         user user=new user();         user.addhistory(new userhistory(new date(),"set name akku"));         user.setname("akku");         user.getproteindata().settotal(234);            user.addhistory(new userhistory(new date(),"set goal 234"));         session.save(user);         session.gettransaction().commit();          session.begintransaction();             user loadedeuser=(user)session.get(user.class,1);          system.out.println(loadedeuser.getname());          system.out.println(loadedeuser.getproteindata().getgoal());          for(userhistory history:loadedeuser.gethistory()){             //system.out.println("key is: "+ history.getkey());             system.out.println(history.getentrytime().tostring()+" "+history.getentry());         }          loadedeuser.getproteindata().settotal(loadedeuser.getproteindata().gettotal()+50);         loadedeuser.addhistory(new userhistory(new date(),"added 32 proteins"));          session.gettransaction().commit();          session.close();         hibernateutility.getsessionfactory().close();     }  } 

user entity

public class user {      private int id;     private string name;       private proteindata proteindata;      private list<userhistory> history=new arraylist<userhistory>();      public user(){          setproteindata(new proteindata());     }      public int getid() {         return id;     }     public void setid(int id) {         this.id = id;     }     public string getname() {         return name;     }     public void setname(string name) {         this.name = name;     }     public proteindata getproteindata() {         return proteindata;     }     public void setproteindata(proteindata proteindata) {         this.proteindata = proteindata;         try{          proteindata.setuser(this);  //null pointer exception here          }catch(exception e){             e.printstacktrace();         }      }      public list<userhistory> gethistory() {         return history;     }     public void sethistory(list<userhistory> history) {         this.history = history;     }      public void addhistory(userhistory historyitem){          historyitem.setuser(this);          history.add(historyitem);     }  } 

proteindata class

 public class proteindata {          private int id;         private user user;         private int total;         private int goal;          public int gettotal() {             return total;         }         public void settotal(int total) {             this.total = total;         }         public int getgoal() {             return goal;         }         public void setgoal(int goal) {             this.goal = goal;         }         public int getid() {             return id;         }         public void setid(int id) {             this.id = id;         }         public user getuser() {             return user;         }         public void setuser(user user) {             this.user = user;         }     } 

i have created setter setting value of proteindata variable in user on passing proteindata.adduser(this) through setter getting null pointer exception

how can solve problem?

thanks

getters , setters called hibernate during entity lifecycle, isn't guaranteed setter won't called null value. avoid error check if parameter null

  public void setproteindata(proteindata proteindata) {       this.proteindata = proteindata;       if (proteindata != null) {           proteindata.setuser(this);       }   } 

Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -