0

我正在处理一个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 + '}';
 }
} 

为什么即使我的类是可序列化的,我也会收到 NotSerializable 错误?现在有人吗?

4

1 回答 1

0

正如其他回答者指出的那样,最可能的原因是您的Pc课程没有实现。Serializable如果不是这种情况,请检查您的类的成员Pc:该类的每个字段也必须是可序列化的,以便主类成为。如果您正在处理继承,还要检查超类的每个字段。

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

于 2012-05-22T09:15:35.030 回答