プログラムを実行すると、次のエラーが発生します。
java.io.InvalidClassException: Food; local class incompatible: stream classdesc serialVersionUID = -1986042743597353721, local class serialVersionUID = -2452322360924937818
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:570)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1599)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1494)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1599)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1494)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1748)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1327)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1683)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1321)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:349)
at GroceryStore.readInventory(GroceryStore.java:16)
at GroceryStore.main(GroceryStore.java:39)
私は非常に多くのことを試しましたが、どこが間違っているのかよくわかりません。このことは初めてです。
import java.io.Serializable;
public abstract class Food implements Serializable {
String Type, Name;
double Price;
private static final long serialVersionUID = -2452322360924937818L;
public Food(String Type, String Name, double Price){
this.Type = Type;
this.Name = Name;
this.Price = Price;
}
public String getType(){
return Type;
}
public String getName(){
return Name;
}
public double getPrice(){
return Price;
}
public String toString(){
return (Type + " " + Name + " " + Price);
}
public abstract double getQuantity();
}
import java.io.Serializable;
public class FoodItem extends Food implements Serializable {
private static final long serialVersionUID = -2452322360924937818L;
private double quantity;
int bestBeforeMonth;
int bestBeforeDay;
public FoodItem(String Type, String Name, double Price, double quantity, int bestBeforeMonth, int bestBeforeDay) {
super(Type, Name, Price);
this.quantity = quantity;
this.bestBeforeDay = bestBeforeDay;
this.bestBeforeMonth = bestBeforeMonth;
}
@Override
public double getQuantity() {
return quantity;
}
public void removeQuantity(double quantity){
this.quantity = this.quantity - quantity;
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectInputStream.GetField;
import java.io.ObjectOutputStream;
public class GroceryStore {
static FoodItem[] inventory = null;
public static void readInventory(){
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("grocery.bin"));
inventory[0] = (FoodItem) in.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
readInventory();
}
}