gpt4 book ai didi

java - 为什么我会收到 java.io.InvalidClassException?

转载 作者:行者123 更新时间:2023-12-02 06:03:53 24 4
gpt4 key购买 nike

我不知道为什么会发生这种情况。

    class Course implements Serializable
{


private String campus; // the campus on which the course is offered
private String course; // the course number, such as CSCI 111
private String section; // the section number
private String crn; // the CRN for this section
private int credits; // the number od credits for the course
private String time; // the time the course is offered, such as 8:00 to 10:00 A.M.
private String days; // the Days the course is offered, suhc as MW


// constructors
Course() {
}

Course(String course, String section, String crn, int credits) {
this.course = course;
this.section = section;
this.crn = crn;
this.credits = credits;
} // end Course() initalizing

// muatator methods

public void setCampus(String cmp) {
this.campus = cmp;
}// end setCampus()

public void setCourse(String crse) {
this.course = crse;
}// end setCourse()

public void setSection(String sect) {
this.section = sect;
} // end setSection()

public void setCRN(String crn) {
this.crn = crn;
} // end setCRN()

public void setCredits(int cr) {
this.credits = cr;
} // end setCredits()

public void setTime(String tm) {
this.time = tm;
}// end setTime()

public void setDays(String days) {
this.days = days;
}// end setDays()


// accessor methods

public String getCampus() {
return campus;
} // end getCampus()

public String getCourse() {
return course;
} // end Course()

public String getSection() {
return section;
} // end getSection()

public String getCRN() {
return crn;
} // end getCRN()

public int getCredits() {
return credits;
} // end getCredits()

public String getTime() {
return time;
} // end getTime()

public String getDays() {
return days;
} // end getDays()


// method to compare by CRN using the String class compareTo()
public int compareTo(Course other) {
return this.crn.compareTo(other.getCRN());
} // end compareTO()

// method to return properties as a string for csv file
// public String toCSVString() {
//
// return credits
// + campus + ","
// + course + ","
// + section + ","
// + crn + " " + ","
// + time + ","
// + days;
//
// } // end toCSVString()

// method to return properties as a string
public String toString() {

return campus + " "
+ course + " "
+ section + " "
+ crn + " "
+ credits + " "
+ time + " "
+ days;

} // end toString()


}// end class Course

当我取消注释 toCSVString() 方法时,我得到 java.io.InvalidClassException

toCSVString 与 toString() 几乎相同,它不起作用对我来说没有任何意义。

这是完整的错误:

java.io.InvalidClassException: writecoursefile.Course; local class incompatible: stream classdesc serialVersionUID = -8471399827293588877, local class serialVersionUID = -3743045684326637976
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:617)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1622)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1706)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1344)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at writecoursefile.WriteCourseFile.readFromFile(WriteCourseFile.java:46)
at writecoursefile.WriteCourseFile.main(WriteCourseFile.java:21)
Exception in thread "main" java.lang.NullPointerException
at writecoursefile.WriteCourseFile.main(WriteCourseFile.java:24)
Java Result: 1

最佳答案

我没有在你的代码中看到你在哪里声明你的serialVersionUID - 但你需要声明它,并使其

例如:

私有(private)静态最终长serialVersionUID = 1L;

这里有一篇很好的文章解释了原因:

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

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