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 に答える