0

I am referencing the Date class in another class which I'm trying to write to an instance of to a file using the CompanySaver class. The Date class is a child of the calendar class which implements java.io.Serializable and has no instance variables besides the 3 ints inherited from the Calendar class. The classes and stack trace are below.

Stack Trace:

java.io.NotSerializableException: Investment.Date
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
        at java.util.ArrayList.writeObject(ArrayList.java:762)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1028)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
        at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
        at Investment.CompanySaver.save(CompanySaver.java:32)
        at Investment.Investment.main(Investment.java:25)

Calendar Class:

public class Calendar implements Serializable
{
    protected int month;
    protected int day;
    protected int year;
    protected int maxDay;

    protected static final long serialVersionUID = 2L;

    public void checkMaxDay()
    {
        if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
            maxDay = 31;
        else if (month == 2)
            maxDay=28;
        else if (year % 4 == 0 && month == 2)
            maxDay=29;
        else 
            maxDay=30;
        if(day>maxDay)
            day = maxDay;
    }

    public void checkMaxMonth()
    {
        if(month>12)
            month=12;
    }
}

Date Class:

package Investment;

public class Date extends Calendar
{
    Date()

    Date(int month, int day, int year)
    {
        if(month<13 && month>0)
            this.month = month;
        else
            throw new IllegalArgumentException("Month must be between 1 and 12");
        if(day<32 && day>0)
            this.day = day;
        else
            throw new IllegalArgumentException("Day must be between 1 and 31");
        if(year>2000)
            this.year = year-2000;
        else 
            this.year = year;
        checkMaxDay();
    }

    public String getDate() {return String.valueOf(month)+"/"+String.valueOf(day)+"/"+String.valueOf(year);}

    public void setDate(int month, int day, int year)
    {
        if(month<13 && month>0)
            this.month = month;
        else
            throw new IllegalArgumentException("Month must be between 1 and 12");

        if(day<32 && day>0)
            this.day = day;
        else
            throw new IllegalArgumentException("Day must be between 1 and 31");

        if(year>2000)
            this.year = year-2000;
        else 
            this.year = year;
        checkMaxDay();
    }

    public int getYear() {return year;}

    public int getMonth() {return month;}

    public int getDay() {return day;}

    public void setYear(int year) {this.year = year;}

    public void setMonth(int month) 
    {
        if(month<13 && month>0)
            this.month = month;
        else
            throw new IllegalArgumentException("Month must be between 1 and 12");
    }

    public void setDay(int day)
    {
        this.day = day; 
        checkMaxDay();
    }

    public boolean equals(Date date)
    {
        if(date.getDay() == this.day && date.getMonth() == this.month && date.getYear() == this.year)
            return true;
        else 
            return false;
    }

    public void subtract(int num, String unit)
    // Does not support +/- days
    {
        if(unit.equalsIgnoreCase("months"))
        {
            if(num<month)
                month = month-num;
            else if (num == month)
            {
                year--;
                month = 12;
            }
            else if (num<12)
            {
                year--;
                month = 12 -(num-month);
            }
            else if (num == 12) 
            {
                year--;
            }
            else 
            {
                year = year - (num/12);
                int num2 = num % 12;

                if(num2<month)
                    month = month-num2;
                else if (num2 == month)
                {
                    year--;
                    month = 12;
                }
                else
                {   
                    year--;
                    month = 12-(num2-month);
                }
            }
            checkMaxDay();
            day=maxDay; 
        }
        if(unit.equalsIgnoreCase("days"))
        {
            System.out.println("Date math does not support adding/subtracting days yet");
        }
        if(unit.equalsIgnoreCase("years"))
        {
            year = year - num;
            if(year<0)
                year = 0;
        }

        checkMaxDay();
        checkMaxMonth();
    }

    public void add(int num, String unit)
    {
        if(unit.equalsIgnoreCase("months"))
        {
            if(num + month <= 12)
                month = num + month;
            else if(num<=12)
            {
                year++;
                month = (month+num)-12;
            }

            else
            {
                int num2 = (num) % 12;
                year = year + (num/12);
                if(month + num2 >= 12)
                {
                    year++;
                    month = (month+num2)-12;
                }
                else
                    month = month + num2;
            }
        }
        if(unit.equalsIgnoreCase("days"))
        {
            System.out.println("Date math does not support adding/subtracting days yet");
        }
        if(unit.equalsIgnoreCase("years"))
        {
            year = year + num;
        }

        checkMaxDay();
        checkMaxMonth();
    }
}

CompanySaver Class:

package Investment;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

public class CompanySaver 
{ 
    private static final String filepath="C:\\Users\\Masiah\\Documents\\GitHUb\\Investments\\Companies\\";

    public static void save(Company comp) 
    {
        try 
        {
            FileOutputStream fileOut = new FileOutputStream(filepath+comp.getName());
            ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
            objectOut.writeObject(comp);
            objectOut.close();
        } 

        catch (Exception ex) 
        {
            ex.printStackTrace();
        }
    }

    public static void save(Retail comp) 
    {
        try 
        {
            FileOutputStream fileOut = new FileOutputStream(filepath+comp.getName());
            ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
            objectOut.writeObject(comp);
            objectOut.close();
        } 

        catch (Exception ex) 
        {
            ex.printStackTrace();
        }
    }
}
Mbando
  • 19
  • 4
  • 1
    Please add full stacktrace. – Jerome Wolff Apr 12 '20 at 19:33
  • Just added the full stack trace – Mbando Apr 12 '20 at 19:53
  • Date class is not compilable. Please post a minimal working example. The parameterless constructor has a syntax problem. Also, please post how you use the serialization. Is it simple writeObject() on a ObjectOutputStream? – Kartal Tabak Apr 12 '20 at 20:35
  • Yes, I added the class which is supposed to save the class but it does use the method you used. The date class inherits all its variables from the Calendar class so I think a minimum working example would be an empty date class (unless there is something I'm missing). – Mbando Apr 12 '20 at 21:08
  • I have tested Date class, and after fixing the constructor, it is working fine. You do have a "Company" class now, on the writeObject usage, and its code is missing. Instead of adding a new class (Company), for the MWE purposes, try to directly serialize a Date object. Also, make Date class compilable. – Kartal Tabak Apr 12 '20 at 21:38
  • How did you fix the constructor? It compiles fine how it is for me. – Mbando Apr 12 '20 at 22:21
  • Does this answer your question? [java.io.NotSerializableException](https://stackoverflow.com/questions/13895867/java-io-notserializableexception) – Jerome Wolff Apr 12 '20 at 22:54
  • 1
    No the solutions for his code cant be applied to mine. I do not have any inner classes or non-serialable variables. – Mbando Apr 12 '20 at 23:00
  • Is your `Calendar` class in the same package as `Date`? Or are you inadvertent extending `java.util.Calendar`? And why are you reusing class names from the JDK? Don't do that, – user207421 Apr 12 '20 at 23:06
  • Yes, they are in the same package and I didn't think about how they have the same name. Thanks. – Mbando Apr 13 '20 at 00:34

1 Answers1

1

Probably you have an out-of-date class. Try cleaning your build directory and rebuild your project.

Kartal Tabak
  • 760
  • 1
  • 7
  • 18