0

I have no idea why this is happening.

    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

When I uncomment the toCSVString() method I get java.io.InvalidClassException

toCSVString is almost identical to toString(), it doesn't make any sense to me that it would not work.

This is the full error:

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
user2809114
  • 97
  • 4
  • 15
  • 2
    It looks like a version that was serialized is different from the version that you're trying to de-serialize it to. Have you changed your class structure between serialization and de-serialization? – Hovercraft Full Of Eels Mar 17 '14 at 03:26
  • And where do you declare the serialVersionUID variable? This is one reason why it is so important to explicitly declare a serialVersionUID. – Hovercraft Full Of Eels Mar 17 '14 at 03:26
  • Well I didnt serialize it in the first place. Its part of a programming asignment where we were provided with a .ser file and we are to read it, sort it and make it into a csv file. also, I did not declare a serialVersionUID, I haven't come across that in my reading. Maybe I don't have enough information to finish this project at the moment. – user2809114 Mar 17 '14 at 03:30
  • 2
    @user2809114: you can't change this class then and expect it to unserialize properly. It must remain the same. – Hovercraft Full Of Eels Mar 17 '14 at 03:31
  • ok, so nothing can change in this class then? If I want to read serialized data made from this class ,I need to keep the class exactly the same as how it was when the .ser file was made? – user2809114 Mar 17 '14 at 03:39
  • 1
    @user2809114 You should be able to change the class if you declare `serialVersionUID` instead of letting the compiler generate it for you. Read the QA I linked to. The assignment's materials should have included this number but it's included in the error message: `-8471399827293588877`. – Radiodef Mar 17 '14 at 03:50
  • hmm. Well using private static final long serialVersionUID = -8471399827293588877; gives me the error that the number is too large (using netbeans). – user2809114 Mar 17 '14 at 05:00

1 Answers1

2

I didn't see in your code where you're declaring your serialVersionUID - but you need to declare it, and make it long.

For example:

private static final long serialVersionUID = 1L;

Here's a good article explaining why:

FoggyDay
  • 11,962
  • 4
  • 34
  • 48