Advertisement

07.25.2007 at 05:31AM PDT, ID: 22719626
[x]
Attachment Details

Strange exception stack trace...

Asked by ksivananth in Java Programming Language

Tags: valid, class, employee

See the below program,

package test;

import java.io.*;
import java.util.Date;

public class HeadQuarterEmpProcessor {
      
      private void serialize(){
            CompanyEmployee emp = new CompanyEmployee();
            emp.lName = "John";
            emp.fName = "Smith";
            emp.salary = 50000;
            emp.address = "12 main street";
            emp.hireDate = new Date();

            FileOutputStream fOut=null;
            ObjectOutputStream oOut=null;

            try{
                  fOut= new FileOutputStream("c:\\NewEmployee.ser");
                  oOut = new ObjectOutputStream(fOut);
                  oOut.writeObject(emp); //serializing employee
                  System.out.println(
                  "An employee is serialized into c:\\NewEmployee.ser");
            }catch(IOException e){
                  e.printStackTrace();
            }finally{
                  try {
                        oOut.flush();
                        oOut.close();
                        fOut.close();
                        emp.fName = "Smith - After Serialization";
                  } catch (IOException e1) {
                        e1.printStackTrace();
                  }
            }
      }
      
      private void deserialize(){
            FileInputStream fIn=null;
            ObjectInputStream oIn=null;

            try{
                  fIn= new FileInputStream("c:\\NewEmployee.ser");
                  oIn = new ObjectInputStream(fIn);

                  //de-serializing employee
                  CompanyEmployee emp = ( CompanyEmployee ) oIn.readObject();

                  System.out.println( "Deserialized " + emp );
            }catch(IOException e){
                  e.printStackTrace();
            }catch(ClassNotFoundException e){
                  e.printStackTrace();
            }finally{
                  try {
                        oIn.close();
                        fIn.close();
                  } catch (IOException e1) {
                        e1.printStackTrace();
                  }
            }
      }

      public static void main(String[] args) {
            HeadQuarterEmpProcessor processor = new HeadQuarterEmpProcessor() ;
            processor.serialize() ;
            processor.deserialize() ;
      }
}

class Employee{
      String lName;
      static String fName;
      transient double salary;
      java.util.Date hireDate;
      String address;
      
      Employee( String name ){
            lName = name ;
      }
}

class CompanyEmployee extends Employee implements java.io.Serializable {
      final int constant ;
      
      CompanyEmployee(){
            super( "sd" ) ;
            constant = 1000 ;
      }
      
      public String toString(){
            StringBuffer buf = new StringBuffer() ;
            
            buf.append( "Last Name: " + lName ) ;
            buf.append( "\nFirst Name: " + fName ) ;
            buf.append( "\nsalary: " + salary ) ;
            buf.append( "\nconstant: " + constant ) ;
            
            return buf.toString() ;
      }
}

I understand there is a error in the program that the Employee class doesn't have default constructor... but the exception stack points a different line or method call for the exception,

java.io.InvalidClassException: test.CompanyEmployee; no valid constructor
      at java.io.ObjectStreamClass.<init>(ObjectStreamClass.java:455)
      at java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:297)
      at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1035)
      at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:302)
      at test.HeadQuarterEmpProcessor.serialize(HeadQuarterEmpProcessor.java:22)
      at test.HeadQuarterEmpProcessor.main(HeadQuarterEmpProcessor.java:67)

See the exception is reported in the serialize method than the deserialize method, why?Start Free Trial
 
Loading Advertisement...
 
[+][-]07.25.2007 at 05:35AM PDT, ID: 19564742

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.25.2007 at 05:39AM PDT, ID: 19564786

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.25.2007 at 06:03AM PDT, ID: 19564942

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.25.2007 at 06:04AM PDT, ID: 19564946

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.25.2007 at 11:54PM PDT, ID: 19572328

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:00AM PDT, ID: 19572356

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:11AM PDT, ID: 19572388

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:15AM PDT, ID: 19572402

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:16AM PDT, ID: 19572404

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:20AM PDT, ID: 19572410

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:20AM PDT, ID: 19572412

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:21AM PDT, ID: 19572416

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:24AM PDT, ID: 19572431

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:27AM PDT, ID: 19572444

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:31AM PDT, ID: 19572461

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:34AM PDT, ID: 19572467

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:39AM PDT, ID: 19572481

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:45AM PDT, ID: 19572500

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:45AM PDT, ID: 19572501

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:48AM PDT, ID: 19572515

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:49AM PDT, ID: 19572518

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:50AM PDT, ID: 19572525

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:52AM PDT, ID: 19572534

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Java Programming Language
Tags: valid, class, employee
Sign Up Now!
Solution Provided By: Bart_Cr
Participating Experts: 3
Solution Grade: A
 
 
[+][-]07.26.2007 at 12:53AM PDT, ID: 19572538

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:54AM PDT, ID: 19572546

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 12:54AM PDT, ID: 19572547

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 01:00AM PDT, ID: 19572571

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 01:03AM PDT, ID: 19572585

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 01:04AM PDT, ID: 19572587

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 01:09AM PDT, ID: 19572618

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 01:13AM PDT, ID: 19572641

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 01:20AM PDT, ID: 19572669

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 01:29AM PDT, ID: 19572711

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 02:12AM PDT, ID: 19572879

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.26.2007 at 02:17AM PDT, ID: 19572897

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32