java - I am getting an Exception while serializing an object -


the exception

java.io.notserializableexception : com.mysql.jdbc.singlebytecharactersetconverter

package business_logics;   import java.util.arraylist; import java.util.iterator;    public class purchase implements java.io.serializable { string client; int purchaseid; string issue; string due; arraylist<particulars> myparticulars=new arraylist(); string personal; double total; double otheramount; double previous; double grandtotal; string paymentmode; double paid; double cbalance;  public purchase(int id,string client, string issue, string due,string personal, double otheramount, double previous,string payment ) {     this.purchaseid=id;     this.client=client;     this.issue=issue;     this.due=due;     this.personal=personal;     this.otheramount= otheramount*(-1);     this.previous=previous;     this.paymentmode=payment;  }  public void settransaction(double paid) {     this.paid=paid;     this.cbalance=this.grandtotal+this.paid; }  public void setparticulars(arraylist<particulars> part) {     part.stream().map((part1) -> {         this.myparticulars.add(part1);         return part1;     }).foreach((part1) -> {         total += part1.giveamount();     });     total=total*(-1);    grandtotal=getgrandtotal(); }  public double getgrandtotal() {     return total+otheramount+previous; } public double gettotal() {     return total; }  public int getpurchaseid() {     return purchaseid; }  public double getbalnace() {     return cbalance; }  public string getisuuedate() {     return issue; }  public string getduedate() {     return issue; }  public double getpaid() {     return paid; }  public double getprevious() {     return previous; } public double getother() {     return otheramount; }  public string fulldetail() {     stringbuilder mydata=new stringbuilder();     mydata.append("------------------------------------------------------------------------------------------");    mydata.append("\n\t\tclient info !!");    mydata.append("\n------------------------------------------------------------------------------------------");    mydata.append("\nname: ").append(client);     mydata.append("\nissue date: "+issue);    mydata.append("\ndue date: "+due);    mydata.append("\n------------------------------------------------------------------------------------------");    mydata.append("\n\t\titem info");    mydata.append("\n------------------------------------------------------------------------------------------");    mydata.append("\n");    int i=1;    (particulars myparticular : myparticulars)    {         mydata.append(i+" | ");         mydata.append(myparticular);         mydata.append("\n");i++;    }    mydata.append("\n------------------------------------------------------------------------------------------");    mydata.append("\n\t\t\t\tnet payable : "+"\u20b9 "+total);    mydata.append("\n------------------------------------------------------------------------------------------");    mydata.append("\n");      mydata.append("\n------------------------------------------------------------------------------------------");    mydata.append("\n\t  other info !!");    mydata.append("\n------------------------------------------------------------------------------------------");    mydata.append("\n");   mydata.append("\nprevious amount: "+previous);    mydata.append("\nother amount: "+otheramount);    mydata.append("\npersonal note : "+personal);    mydata.append("\npayment mode: "+paymentmode);    mydata.append("\ntotal net payable :"+"\u20b9 "+grandtotal);     mydata.append("\n");    mydata.append("\n------------------------------------------------------------------------------------------");    mydata.append("\n");    mydata.append("\namount paid:"+"\u20b9 "+paid);    mydata.append("\n");    mydata.append("\ncurrent balance:"+"\u20b9 "+cbalance);     mydata.append("\n------------------------------------------------------------------------------------------");    return mydata.tostring(); }  public boolean updatequantity() throws exception {     boolean done=true;     int number=myparticulars.size();      iterator<particulars> iterate=myparticulars.iterator();      int count=0;     while(iterate.hasnext())     {         count+=iterate.next().updateparticularquantity();     }      if(number!=count)     {         done=false;     }     return done; }   public string getpersonal() {    return personal; }  } 

this class trying serialize outside this

   public void saveinvoice(purchase p) throws  exception   {      connect.checkfolderexistence();// working fine !!      string path="c:\\techbill\\purchaseinvoices";      file location=new file(path);      if(!(location.exists() && location.isdirectory()))      {         location.mkdir();         savebill(path,p);      }     else     {        savebill(path,p);     }   }   private void savebill(string path,purchase p) throws exception   {          path=path+"\\"+purchaseid+".ser";         file myfile=new file(path);          fileoutputstream fout=new fileoutputstream(myfile);         objectoutputstream obout=new objectoutputstream(fout);         obout.writeobject(p);         obout.close();         fout.close();  }  public void makepurchase(int client_id,purchase p) throws exception {     persistpurchase(client_id,p);     transaction purchasetransaction=     new transaction(p.getisuuedate(),p.getgrandtotal(), client_id,         p.getpurchaseid(), p.getpaid());     purchasetransaction.maketransaction(false, 1); //working fine in other same case     updatebalance(client_id,p);     saveinvoice(p); } 

calling methods in button action

     try       {         double oamt=double.parsedouble(other.gettext());          string mode=credit.isselected() ? "credit" : "debit";          double previ=double.parsedouble(prev.gettext());         if(prev.getforeground()!=green)         {             previ=previ*(-1);         }           purchase purchased=new purchase(integer.parseint(pid.gettext()),client.getselecteditem().tostring(),             formatdate(issue.getdate()),formatdate(due.getdate()),notes.gettext(),oamt,previ,mode);           purchased.setparticulars(myparticulars());           if(!mode.equalsignorecase("credit"))         {              purchased.settransaction(double.parsedouble(pai.gettext()));         }         else         {             purchased.settransaction(double.parsedouble("0.0"));         }           string clientname=getclientname(client.getselecteditem().tostring()).trim();         string clientcity=getclientcity(client.getselecteditem().tostring()).trim();           system.out.println(purchased.fulldetail());         if(purchased.updatequantity())         {             makepurchase(getclientid(clientname, clientcity), purchased);               joptionpane.showmessagedialog(this,"quantity updated");             //jasper report....         }         else         {             joptionpane.showmessagedialog(this,"quantity not updated");         }     }     catch (exception e)      {         e.printstacktrace();     } 

the code related database manipulation , fetching follows:

private void persistpurchase(int id,purchase p) throws exception {     string query="insert purchase values(?,?,?,?,?,?,?,?,?)";      preparedstatement ps=connect.giveconnection().preparestatement(query);      ps.setint(1, purchaseid);     ps.setint(2, id);     ps.setstring(3, todatabase(p.getisuuedate()));     ps.setstring(4, todatabase(p.getduedate()));     ps.setint(5, particulartable.getmodel().getrowcount());     ps.setdouble(6, p.getprevious());     ps.setdouble(7, p.getother());     ps.setdouble(8, p.getgrandtotal());     ps.setstring(9, p.getpersonal());     ps.executeupdate(); }  public void updatebalance(int id,purchase p) throws exception {     string query="update net_balance set balance="+p.getbalnace()+"where           client_id="+id;     statement stmt=new            connectionseeker().giveconnection().createstatement();     stmt.executeupdate(query); }  

the maketransaction() method defined in transaction class , method working in other case..

public void maketransaction(boolean type,int i) throws exception //type    specifies sell-  true(credit) & purchase- false(debit).... {    string query="insert stransaction values(?,?,?,?,?,?,?,?,?)";      preparedstatement ps=new      connectionseeker().giveconnection().preparestatement(query);      int no=fetchtransid();      ps.setstring(1,todatabase(issuedate));     ps.setstring(2,getbilltype(i));     ps.setint(3,bill_id);     ps.setint(4,client_id);     ps.setint(5,no);     ps.setdouble(6,amount);      double cbalance=0;//amount-saveamount;      if(type)     {         cbalance=cbalance+(amount-saveamount);         ps.setdouble(7,0.0);         ps.setdouble(8,round(saveamount, 2));         ps.setdouble(9,round(cbalance,2));     }     else     {         cbalance=cbalance+(amount+saveamount);         ps.setdouble(7,saveamount);         ps.setdouble(8,0.0);         ps.setdouble(9, round(cbalance, 2));     }      ps.executeupdate(); } 

the exception occuring while execution of makepurchase() in button click inside if(purchased.updatequantity())

i getting exception can please annybody tell me root cause of occurence of same. in advance.

  1. the simplest solution find class throws exception , make implement serializable interface. however, may not feasible if class throws exception belongs third-party library.

  2. in case class refers non-serializable objects , these objects should not serialized, then, can declare these objects transient. once field of class declared transient, then, ignored serializable runtime.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -