-1

For some reason this object cannot be serialized, it used to work but when I use the constructor with the arraylist it started to through error, and now the problem is that when serializing a, there's also an error...

This is the constructor:

private double cost;
private char InputType, CurrencyType;
int Year, Month, Day, MonthlyTotal;

ArrayList<FinanceDailyRecord> setDailyRecord;



public FinanceDailyRecord(double cost, char InputType, char CurrencyType, int Year, int Month, int Day) {
    this.cost = cost;
    this.InputType = InputType;
    this.CurrencyType = CurrencyType;
    this.Year = Year;
    this.Month = Month;
    this.Day = Day;
}

public FinanceDailyRecord(int Day, ArrayList<FinanceDailyRecord> setDailyRecord) {
    this.Day = Day;
    this.setDailyRecord = setDailyRecord;
}

This is the serialization part

    ArrayList<FinanceDailyRecord> RecordArray = new ArrayList<>();
    FinanceDailyRecord dailyRecord = new FinanceDailyRecord(22,RecordArray);
    System.out.println(dailyRecord);
    FinanceDailyRecord a  = new FinanceDailyRecord(9.0, 'a','a',12,1,212);

    FileOutputStream out = new FileOutputStream("test.bin");
    ObjectOutputStream obout = new ObjectOutputStream(out);
    obout.writeObject(a);
    obout.close();

This is the error:

Exception in thread "main" java.io.NotSerializableException: com.company.FinanceDailyRecord
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at com.company.Main.main(Main.java:23)
  • 1
    Does this answer your question? [Why does writeObject throw java.io.NotSerializableException and how do I fix it?](https://stackoverflow.com/questions/13895867/why-does-writeobject-throw-java-io-notserializableexception-and-how-do-i-fix-it) – maloomeister Jul 22 '21 at 05:31
  • Please provide a [mre]. The error indicates that `FinanceDailyRecord` is not serializable, because it does not implement `Serializable`, but you aren't showing the class definition itself. – Mark Rotteveel Jul 22 '21 at 05:31
  • Omg I forgot to implement Serializable – JVAV_master Jul 22 '21 at 05:40

1 Answers1

0
Exception in thread "main" java.io.NotSerializableException: com.company.FinanceDailyRecord

FinanceDailyRecord Should be Serializable. Please implement an interface on FinanceDailyRecord class.

VedantK
  • 9,728
  • 7
  • 66
  • 71