我有一个实现Serializable的类,但是每当我尝试将它写入磁盘时,我都无法找到它抛出此异常的原因。我正在尝试编写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);
}
}
}
答案 0 :(得分:4)
我只是想在黑暗中拍摄并说......
MonetaryField
也需要Serializable。
答案 1 :(得分:1)
...遍历图表时, 可能遇到不支持Serializable的对象 接口。在这种情况下,将抛出NotSerializableException 并将识别非序列化对象的类。
来自javadocs。要使您的实例可序列化,对象图中的所有对象也必须是可序列化的。仔细检查是否是这种情况。
答案 2 :(得分:0)
如果类实现serializable
并且其所有非原始字段都是serializable
,那么它是可序列化的。可能MonetaryField
不是serializable
。