Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Runtime exception (Invalid Class exception) occurs. Please help

  1. #1
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Runtime exception (Invalid Class exception) occurs. Please help

    Hi
    I am new to this forum and would appreciate any help that can be given. The following code is for an exercise in a book I am reading. It compiles fine but produces the following exception when executed.


    java.io.InvalidClassException: Loan; local class incompatible: stream classdesc
    serialVersionUID = -5273567861794013902, local class serialVersionUID = 19442595
    53328340694
    at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
    at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
    at java.io.ObjectInputStream.readClassDesc(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unkno wn Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at Exercise19_7.main(Exercise19_7.java:14)



    Thanks in advance for any help.


    Here is the code.

    import java.io.*;
    import java.util.*;
     
    public class Exercise19_7 {
     
      public static void main(String[] args) throws ClassNotFoundException {
        	ArrayList<Loan> loanList = new ArrayList<Loan>();
    	try{
              ObjectInputStream input = new ObjectInputStream(new FileInputStream("Exercise19_6.dat"));
    	  while(true){
    	    Loan loan = (Loan)(input.readObject());
    	    loanList.add(loan);
    	  }
    	}
    	catch (EOFException ex) {
    		for(int i = 0; i < loanList.size(); i++){
    		  System.out.println("Loan Date: " + ((Loan)(loanList.get(i))).getLoanDate());
    		  System.out.println("Loan Amount: " + ((Loan)(loanList.get(i))).getLoanAmount());
    		  System.out.println("Interest Rate: " + ((Loan)(loanList.get(i))).getAnnualInterestRate());
    		  System.out.println("Number of years: " + ((Loan)(loanList.get(i))).getNumberOfYears());
    		  System.out.println("Total Payment: " + ((Loan)(loanList.get(i))).getTotalPayment());
    		}
            }
     
    	catch (IOException ex) {
    	  ex.printStackTrace();
    	}
     
      }
     
    }

    Here is the Loan class. It has worked fine in other exercises.

    import java.util.Date;
    import java.io.*;
    public class Loan implements Serializable {
     
      private double annualInterestRate;
      private int numberOfYears;
      private double loanAmount;
      private Date loanDate;
     
      public Loan() {
        this(2.5, 1, 1000);
     
      }
     
      public Loan(double annualInterestRate, int numberOfYears, 
          double loanAmount) throws IllegalArgumentException {
     
        if (annualInterestRate <= 0)
            throw new IllegalArgumentException("Interest rate must be more than zero");
        if (numberOfYears <= 0) 
            throw new IllegalArgumentException("Number of years must be more than zero");
        if (loanAmount <= 0)
            throw new IllegalArgumentException("Loan Amount must be more than zero");
        this.annualInterestRate = annualInterestRate;
        this.numberOfYears = numberOfYears;
        this.loanAmount = loanAmount;
        loanDate = new Date();
     
      }
     
      public Loan(Loan loan){
        this.annualInterestRate = loan.getAnnualInterestRate();
    	this.numberOfYears = (int)(loan.getNumberOfYears());
    	this.loanAmount = loan.getLoanAmount();
    	this.loanDate = loan.getLoanDate();
     
     
      }
     
      public double getAnnualInterestRate() {
        return annualInterestRate;
     
      }
     
      public void setAnnualInterestRate(double annualInterestRate) throws IllegalArgumentException {
     
        if (annualInterestRate <= 0)
            throw new IllegalArgumentException("Interest rate must be more than zero");
     
        this.annualInterestRate = annualInterestRate;
     
      }
     
      public double getNumberOfYears() {
        return numberOfYears;
     
      }
     
      public void setNumberOfYears(int numberOfYears) throws IllegalArgumentException {
     
        if (numberOfYears <= 0) 
            throw new IllegalArgumentException("Number of years must be more than zero");
     
        this.numberOfYears = numberOfYears;
     
      }
     
      public double getLoanAmount() {
        return loanAmount;
     
      }
     
      public void setLoanAmount(double loanAmount) throws IllegalArgumentException {
     
        if (loanAmount <= 0)
            throw new IllegalArgumentException("Loan Amount must be more than zero");
     
        this.loanAmount = loanAmount;
     
      }
     
     
      public double getMonthlyPayment() {
        double monthlyInterestRate = annualInterestRate / 1200;
        double monthlyPayment = loanAmount * monthlyInterestRate / (1 -
          (Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));
        return monthlyPayment;
     
      }
     
      public double getTotalPayment() {
        double totalPayment = getMonthlyPayment() * numberOfYears * 12;
        return totalPayment;
     
      }
     
     
      public Date getLoanDate() {
     
        return loanDate;
     
      }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,077
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Runtime exception (Invalid Class exception) occurs. Please help

    InvalidClassException: Loan; local class incompatible: stream classdesc
    c = -5273567861794013902,
    local class serialVersionUID = 1944259553328340694
    The text of the error message seems to say that the local class definition is not compatible with what is being read. Notice the serialVersionUIDs are different
    What is the history of the class object and the serialized class? Are they from the same version of the class?

    Add a serialVersionUID to the class so it doesn't generate a new one every time the code is compiled

  3. The Following User Says Thank You to Norm For This Useful Post:

    kc120us (September 4th, 2011)

  4. #3
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Runtime exception (Invalid Class exception) occurs. Please help

    Quote Originally Posted by Norm View Post
    The text of the error message seems to say that the local class definition is not compatible with what is being read. Notice the serialVersionUIDs are different
    What is the history of the class object and the serialized class? Are they from the same version of the class?

    Add a serialVersionUID to the class so it doesn't generate a new one every time the code is compiled
    Thanks for the reply. I am a newbie to Java so please forgive my ignorance. I created the Loan class several chapters ago and modified it for this exercise. It was not serialized prior to this so I added the "implements Serializable" code and a new Constructor to accept a Loan object as an argument. The chapter I am currently reading covers Binary I/O and introduces serialization.

    How do I add a serialVersionUID to the class?

  5. #4
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Runtime exception (Invalid Class exception) occurs. Please help

    ok, I copied the stream serialVersionUID -5273567861794013902 and added it to a static final long variable in the Loan class and now the program is working. Thanks. I am new to serialization. Any thoughts as to why this happened? The book has not mentioned anything about serialVersionUID. I assume it is beyond the scope of an introductory textbook.

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,077
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Runtime exception (Invalid Class exception) occurs. Please help

    The serial ID is to allow the author of the program to tell the system when a new version of the class has been created that is NOT compatible with an older version. You would change it when you did not want someone to use an invalid, older version of a serialized class.

  7. #6
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Runtime exception (Invalid Class exception) occurs. Please help

    Quote Originally Posted by Norm View Post
    The serial ID is to allow the author of the program to tell the system when a new version of the class has been created that is NOT compatible with an older version. You would change it when you did not want someone to use an invalid, older version of a serialized class.
    Thanks again, Norm

Similar Threads

  1. Replies: 8
    Last Post: August 9th, 2011, 08:25 PM
  2. What is Wrong With My Exception Class Code?
    By Allicat in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 5th, 2011, 10:09 AM
  3. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM
  4. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 11:13 AM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM