0

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

"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..

//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?

nick
  • 323
  • 1
  • 4
  • 8

1 Answers1

0

The most probable cause is that you Pc class does not implement Serializable, as other answereres noted. If that's not the case, check the members of your Pc class: every field of that class must also be serializable for the main class to be. Check also every field of the superclass, if you're dealing with inheritance.

Update: ah, just seen the code of the classes was provided also (didn't see before because of messy indentation), and everything is fine with the fields. Maybe the problem is related to serialVersionUID then? I see your class does not have one. Are you trying to load the objects in the same environment where your saved them?

mgibsonbr
  • 21,755
  • 7
  • 70
  • 112
  • hello and thanks for your answer...yes in the same environment – nick May 22 '12 at 09:27
  • I'm a bit confused. Are you having this problem when **loading** or **saving** the objects? The stack trace suggest its a problem with writing, but I don't see any writing being performed in your provided code. Also, please double-check that the code being run is up-to-date (i.e. that the compiled code you're trying to run corresponds to your current source). I know it sounds silly, but since there are no visible problems looking at your sources, this possibility becomes more likely. – mgibsonbr May 22 '12 at 09:42