gpt4 book ai didi

java - 为什么我会收到 NotSerializedException?

转载 作者:行者123 更新时间:2023-12-01 17:22:01 27 4
gpt4 key购买 nike

我有一个实现可序列化的类,但每当我尝试将其写入磁盘时,我都无法找到它抛出此异常的原因。我正在尝试编写 ValueConatiner.class

public class ValueContainer implements Serializable {
private static final long serialVersionUID = 2846820178517499793L;
public String name;
public MonetaryField buy;
public MonetaryField sell;
public MonetaryField revenue;
public MonetaryField cost;
public MonetaryField listing_fee;
public MonetaryField sale_fee;
public MonetaryField profit;
public int quantity;
public int investment;
public int period;

public ValueContainer(String n, MonetaryField b, MonetaryField s,
MonetaryField r, MonetaryField c, MonetaryField lf,
MonetaryField sf, MonetaryField p, int q,
int i, int per) {
name = n;
buy = b;
sell = s;
revenue = r;
cost = c;
listing_fee = lf;
sale_fee = sf;
profit = p;
quantity = q;
investment = i;
try {
period = per;
} catch(NumberFormatException e) {
System.out.println("No data. Enter a number.");
}
}
}

我尝试在一个单独的类中使用此方法将其写入磁盘,我在该类中保存了 JMenuBar。

private void saveFile() {
if(!currentFile.exists()) {
loadFile();
}
if(currentFile.exists()) {
try {
ValueContainer values = getValues();
FileOutputStream f_out = new FileOutputStream(currentFile);
ObjectOutputStream obj_out = new ObjectOutputStream(f_out);
try {
obj_out.writeObject(values);
} finally {
obj_out.close();
f_out.close();
}
System.out.println("Saved!");
} catch(IOException e) {
System.out.println(e);
}
}
}

最佳答案

我只是想在黑暗中试一试然后说......

MonetaryField 也需要可序列化。

关于java - 为什么我会收到 NotSerializedException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18025050/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com