为什么我得到java.io.NotSerializableException:Investment.Date,尽管实现了Serializable?

为什么我得到java.io.NotSerializableException:Investment.Date,尽管实现了Serializable?,java,inheritance,exception,serialization,notserializableexception,Java,Inheritance,Exception,Serialization,Notserializableexception,我正在引用另一个类中的Date类,我正试图使用companyaver类将该类的实例写入文件。Date类是calendar类的子类,该类实现了java.io.Serializable,除了从calendar类继承的3个整数外,没有任何实例变量。下面是类和堆栈跟踪 堆栈跟踪: java.io.NotSerializableException: Investment.Date at java.io.ObjectOutputStream.writeObject0(ObjectOutput

我正在引用另一个类中的
Date
类,我正试图使用
companyaver
类将该类的实例写入文件。Date类是calendar类的子类,该类实现了
java.io.Serializable
,除了从
calendar
类继承的3个整数外,没有任何实例变量。下面是类和堆栈跟踪

堆栈跟踪:

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)
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;
    }
}
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();
        }
    }
}
日历类:

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)
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;
    }
}
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();
        }
    }
}
日期类别:

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();
    }
}

也许你的课程已经过时了。尝试清理生成目录并重建项目

请添加完整stacktrace。刚刚添加的完整stack traceDate类不可编译。请发布一个简单的工作示例。无参数构造函数有语法问题。此外,请张贴如何使用序列化。是ObjectOutputStream上的simple writeObject()吗?是的,我添加了应该保存该类的类,但它确实使用了您使用的方法。date类从Calendar类继承了它的所有变量,所以我认为最起码的工作示例应该是一个空的date类(除非我遗漏了什么)。不,他的代码的解决方案不能应用于我的代码。我没有任何内部类或不可序列化的变量。我只是尝试了一下,但出现了相同的异常。