3 Replies Latest reply: Aug 21, 2011 8:31 AM by EJP RSS

    Externalizable Problem (getting invalidcalassCast exception)

    822437
      Hi

      I am trying to write a simple program to get to know how we can use Externalizable interface. But i got the exception "java.io.InvalidClassException"

      here is my code
      /*Implemention of serializable interface is neccessary otherwise serialization can't be done
      Here we are using Address object in the employee class so it is neccessary in the employeee class also*/
      
      
      import java.io.*;
      public class myExternal2 {
      
      public static void main(String args[]){
      try{
           Address addres=new Address(123);
           ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(new File("myFile.dat"))));
           
           System.out.println("--------Writing------------------");
           oos.writeObject(addres);
           oos.flush();
           System.out.println("------------Reading--------------");
           ObjectInputStream ois=new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File("myFile.dat"))));
           employee e2=(employee)ois.readObject();
           e2.printMyAddress();
           }
           catch(Exception ee){
                System.out.println(ee);
           }
           }
           
      public void printMyAddress(){
                //System.out.println(" Id:: "+id+" Nmae:: "+name+ " Addrss ::"/*+adds.toStrings()*/);
           }
      }
      
      class Address implements Externalizable{
      
      int streetNo;
      Address(int x){
      
      System.out.println("Called.....");
      streetNo=x;
      }
      public int toStrings(){
      return streetNo;
      }
      
      public void writeExternal(ObjectOutput oo)throws java.io.IOException{
           oo.writeInt(streetNo);
           System.out.println("1111111111111111111111111");
           //oo.writeObject(adds);
      }     
      
          /*public abstract void writeExternal(java.io.ObjectOutput)       throws java.io.IOException;*/
      public  void readExternal(java.io.ObjectInput oi)       throws java.io.IOException, java.lang.ClassNotFoundException{
           streetNo=oi.readInt();
      
           //System.out.println("1111111111111111111111111");
           //adds=(Address)oi.readObject();
           }
      
      }
      The exception i got
      F:\Roomies\Gourav\Study\java\Externlzation>java myExternal2
      Called.....
      --------Writing------------------
      1111111111111111111111111
      ------------Reading--------------
      java.io.InvalidClassException: Address; Address; no valid construct
      i search over the google and go through many forums but didn't get the any idea's why i facing this problem.
      Any help :(

      Thanks in advance
        • 1. Re: Externalizable Problem (getting invalidcalassCast exception)
          836548
          Gourav wrote:
          Hi

          I am trying to write a simple program to get to know how we can use Externalizable interface. But i got the exception "java.io.InvalidClassException"

          here is my code
          /*Implemention of serializable interface is neccessary otherwise serialization can't be done
          Here we are using Address object in the employee class so it is neccessary in the employeee class also*/
          
          
          import java.io.*;
          public class myExternal2 {
          
          public static void main(String args[]){
          try{
               Address addres=new Address(123);
               ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(new File("myFile.dat"))));
               
               System.out.println("--------Writing------------------");
               oos.writeObject(addres);
               oos.flush();
               System.out.println("------------Reading--------------");
               ObjectInputStream ois=new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File("myFile.dat"))));
               employee e2=(employee)ois.readObject();
               e2.printMyAddress();
               }
               catch(Exception ee){
                    System.out.println(ee);
               }
               }
               
          public void printMyAddress(){
                    //System.out.println(" Id:: "+id+" Nmae:: "+name+ " Addrss ::"/*+adds.toStrings()*/);
               }
          }
          
          class Address implements Externalizable{
          
          int streetNo;
          Address(int x){
          
          System.out.println("Called.....");
          streetNo=x;
          }
          public int toStrings(){
          return streetNo;
          }
          
          public void writeExternal(ObjectOutput oo)throws java.io.IOException{
               oo.writeInt(streetNo);
               System.out.println("1111111111111111111111111");
               //oo.writeObject(adds);
          }     
          
          /*public abstract void writeExternal(java.io.ObjectOutput)       throws java.io.IOException;*/
          public  void readExternal(java.io.ObjectInput oi)       throws java.io.IOException, java.lang.ClassNotFoundException{
               streetNo=oi.readInt();
          
               //System.out.println("1111111111111111111111111");
               //adds=(Address)oi.readObject();
               }
          
          }
          The exception i got
          F:\Roomies\Gourav\Study\java\Externlzation>java myExternal2
          Called.....
          --------Writing------------------
          1111111111111111111111111
          ------------Reading--------------
          java.io.InvalidClassException: Address; Address; no valid construct
          i search over the google and go through many forums but didn't get the any idea's why i facing this problem.
          Any help :(

          Thanks in advance
          There are couple of issues in your program:
          1) You are trying to access the constructor outside the Address class, first make it public.
          2) There is not default constructor in Address class, so write a default constructor and make it also public.
          3) In above program i couldn't see class employee, then what the below line is for

          >
               employee e2=(employee)ois.readObject();
          i am imagining it is Address, so make it as :
          Address e2 (Address)ois.readObject();
          • 2. Re: Externalizable Problem (getting invalidcalassCast exception)
            822437
            Hi

            Thanks, it works. And it is the employee class :)

            just one thing why we need public with constructor and why no-arg default constructor is required.

            Thanks,
            Gourav
            • 3. Re: Externalizable Problem (getting invalidcalassCast exception)
              EJP
              Because the Object Serialization Specification says so.