By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,606 Members | 2,019 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,606 IT Pros & Developers. It's quick & easy.

What can i do with this error “java.io.NotSerializableException”?

P: 1
i am working on a java project and i want in the begging of the programme to load the things from the file and then add some things... I am getting this error

Expand|Select|Wrap|Line Numbers
  1. "java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException:      pcshop.Pc 
  2.  
  3. at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
  4. at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
  5. at java.util.ArrayList.readObject(ArrayList.java:733)
  6. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  7. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  8. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  9. at java.lang.reflect.Method.invoke(Method.java:601)
  10. at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1004)
  11. at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1866)
  12. at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
  13. at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
  14. at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
  15. at pcshop.PcShop.loadPcFiles(PcShop.java:48)
  16. at pcshop.PcShop.main(PcShop.java:212)
  17.   Caused by: java.io.NotSerializableException: pcshop.Pc
  18. at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
  19. at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
  20. at java.util.ArrayList.writeObject(ArrayList.java:710)
  21. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  22. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  23. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  24. at java.lang.reflect.Method.invoke(Method.java:601)
  25. at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:975)
  26. at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480)
  27. at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
  28. at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
  29. at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
  30. at pcshop.PcShop.main(PcShop.java:255)"
I am gonna give you some code..
Expand|Select|Wrap|Line Numbers
  1. //add the elements to an array list
  2. public static void Insert()
  3.                          Pc pc1=new Pc();
  4.                          System.out.println("Price: \n");
  5.                          n=in.nextFloat();
  6.                          pc1.setPrice(n);
  7.                          System.out.println("Quantity: \n");
  8.                          m=in.nextInt();
  9.                          pc1.setQuantity(m);
  10.                          pcList.add(pc1);//pcList is an array list of Pc class
  11.  }
  12.  //load the file
  13.   public static void loadPcFiles()
  14. {
  15.   try{
  16.  
  17.  
  18.   FileInputStream input1=new FileInputStream("Users.txt");
  19.  
  20.   ObjectInputStream ob1=new ObjectInputStream(input1);
  21.   //pcList is an array list of PC
  22.   pcList =( ArrayList<Pc>) ob1.readObject();
  23.  
  24.  
  25.   ob1.close();
  26.  
  27.   input1.close();
  28.  
  29.   } catch (ClassNotFoundException e) {
  30.           e.printStackTrace();
  31.   } catch(FileNotFoundException e) {
  32.            e.printStackTrace();
  33.    } catch (IOException e) {
  34.   e.printStackTrace();}
  35.  }
  36.  
  37. //print the lements from the arraylist
  38. private static void PrintProducts ()
  39. {
  40. System.out.println("---------------------");
  41. System.out.println("|  pc              :  |\n");
  42. System.out.println("---------------------");
  43.    for(Pc A: pcList)
  44.    {
  45.  
  46.        A.printPc();
  47.    }
  48.  }
  49.  
  50. //PC class
  51. public class Pc  extends Products implements Serializable {
  52.  
  53.  private String type;
  54.  private float frequency;
  55.  
  56.  public Pc() {}
  57.  
  58. public Pc(String type, float frequency) {
  59.     this.type = type;
  60.     this.frequency = frequency;
  61. }
  62.  //and the products class
  63.  public class Products implements Serializable{
  64.  
  65. private float price;
  66. private int quantity;
  67. private int id;
  68. private String description;
  69.  
  70. public Products(){}
  71.  
  72. public String getDescription() {
  73.     return description;
  74. }
  75.  
  76. public void setDescription(String description) {
  77.     this.description = description;
  78. }
  79.  
  80. public float getPrice() {
  81.     return price;
  82. }
  83.  
  84. public void setPrice(float price) {
  85.     this.price = price;
  86. }
  87.  
  88. public int getQuantity() {
  89.     return quantity;
  90. }
  91.  
  92. public void setQuantity(int quantity) {
  93.     this.quantity = quantity;
  94. }
  95.  
  96. public int getId() {
  97.     return id;
  98. }
  99.  
  100. public void setId(int id) {
  101.     this.id = id;
  102. }
  103.  
  104.  @Override
  105.  public String toString() {
  106.      return "Products{" + "price=" + price + ", quantity=" + quantity + ", id=" + id +        ", description=" + description + '}';
  107. }
  108.  
  109.  
  110.  
  111. }
  112.  
  113.  
  114. public float getFrequency() {
  115.     return frequency;
  116. }
  117.  
  118. public void setFrequency(float frequency) {
  119.     this.frequency = frequency;
  120. }
  121.  
  122. public String getType() {
  123.     return type;
  124. }
  125.  
  126. public void setType(String type) {
  127.     this.type = type;
  128. }
  129.  
  130.  public void printPc()
  131.  {
  132.   System.out.println("Price: "+getPrice()+"Quantity"+getQuantity());
  133.  
  134.   System.out.println("---------------------");
  135.  }
  136.  
  137.  @Override
  138. public String toString() {
  139.     return "Pc{" + "type=" + type + ", frequency=" + frequency + '}';
  140.  }
Why do i get an NotSerializable error even though my classes are serializabled??Does anyone now?
May 22 '12 #1
Share this Question
Share on Google+
1 Reply


P: 14
Have you tried running in debug mode to see where the break occurs?
May 23 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.