我怎么处理这个错误“java.io.NotSerializableException”?

时间:2012-05-22 09:11:23

标签: java

我正在研究一个java项目,我想在程序的乞讨中加载文件中的东西,然后添加一些东西......我收到此错误

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

我会给你一些代码..

//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 + '}';
 }
} 

为什么即使我的类是serializabled我也会收到NotSerializable错误?现在有人吗?

1 个答案:

答案 0 :(得分:0)

最可能的原因是,Pc课程没有实施Serializable,正如其他答案所述。如果不是这样,请检查Pc类的成员:该类的每个字段也必须是主类的可序列化。如果你正在处理继承,请检查超类的每个字段。

更新:啊,刚刚看到类的代码也提供了(因为杂乱的缩进而没有看到),并且字段的一切都很好。也许问题与serialVersionUID有关?我看到你的班级没有。您是否尝试在保存它们的相同环境中加载对象?