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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nick86
    New Member
    • May 2012
    • 1

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

    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

    Code:
    "java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException:      pcshop.Pc 
    
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    at java.util.ArrayList.readObject(ArrayList.java:733)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1004)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1866)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    at pcshop.PcShop.loadPcFiles(PcShop.java:48)
    at pcshop.PcShop.main(PcShop.java:212)
      Caused by: java.io.NotSerializableException: pcshop.Pc
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
    at java.util.ArrayList.writeObject(ArrayList.java:710)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:975)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
    at pcshop.PcShop.main(PcShop.java:255)"
    I am gonna give you some code..
    Code:
    //add the elements to an array list
    public static void Insert()
    { 
                             Pc pc1=new Pc();
                             System.out.println("Price: \n");
                             n=in.nextFloat();
                             pc1.setPrice(n);
                             System.out.println("Quantity: \n");
                             m=in.nextInt();
                             pc1.setQuantity(m);
                             pcList.add(pc1);//pcList is an array list of Pc class
     }
     //load the file
      public static void loadPcFiles()
    {
      try{
    
    
      FileInputStream input1=new FileInputStream("Users.txt");
    
      ObjectInputStream ob1=new ObjectInputStream(input1);
      //pcList is an array list of PC
      pcList =( ArrayList<Pc>) ob1.readObject();
    
    
      ob1.close();
    
      input1.close();
    
      } catch (ClassNotFoundException e) {
              e.printStackTrace();
      } catch(FileNotFoundException e) {
               e.printStackTrace();
       } catch (IOException e) {
      e.printStackTrace();}
     }
    
    //print the lements from the arraylist
    private static void PrintProducts ()
    {
    System.out.println("---------------------");
    System.out.println("|  pc              :  |\n");
    System.out.println("---------------------");
       for(Pc A: pcList)
       {
    
           A.printPc();
       }
     }
    
    //PC class
    public class Pc  extends Products implements Serializable {
    
     private String type;
     private float frequency;
    
     public Pc() {}
    
    public Pc(String type, float frequency) {
        this.type = type;
        this.frequency = frequency;
    }
     //and the products class
     public class Products implements Serializable{
    
    private float price;
    private int quantity;
    private int id;
    private String description;
    
    public Products(){}
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public float getPrice() {
        return price;
    }
    
    public void setPrice(float price) {
        this.price = price;
    }
    
    public int getQuantity() {
        return quantity;
    }
    
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
    
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
     @Override
     public String toString() {
         return "Products{" + "price=" + price + ", quantity=" + quantity + ", id=" + id +        ", description=" + description + '}';
    }
    
    
    
    }
    
    
    public float getFrequency() {
        return frequency;
    }
    
    public void setFrequency(float frequency) {
        this.frequency = frequency;
    }
    
    public String getType() {
        return type;
    }
    
    public void setType(String type) {
        this.type = type;
    }
    
     public void printPc()
     {
      System.out.println("Price: "+getPrice()+"Quantity"+getQuantity());
    
      System.out.println("---------------------");
     }
    
     @Override
    public String toString() {
        return "Pc{" + "type=" + type + ", frequency=" + frequency + '}';
     }
    }
    Why do i get an NotSerializable error even though my classes are serializabled?? Does anyone now?
  • firexfighterx
    New Member
    • May 2012
    • 14

    #2
    Have you tried running in debug mode to see where the break occurs?

    Comment

    Working...