Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:06:37+00:00 2026-06-11T11:06:37+00:00

I have been trying to do a little project that needs an appendable ObjectOutputStream.

  • 0

I have been trying to do a little project that needs an appendable ObjectOutputStream.
I have gone through a couple of solutions and i found this It seemed to solve my problem at first. But on further development of my project i started getting unexpected exceptions.
The following are my classes.

public class PPAccount implements Serializable
{
    private Profile profile;
    private String email;
    private float accountBal;
    private boolean isActivated;
    private String activationCode;
    private ArrayList<Transaction> transactions;

    //a few functions   
}
public class PPRestrictedAccount extends PPAccount {
    private String parentEmail;
    private float withdrawLimit;

        //a few functions
}
public class PPBusinessAccount extends PPAccount {
    private ArrayList <PPRestrictedAccount> accountOperators;

        //a few functions
}
public class PPStudentAccount extends PPAccount {
    private String parentEmail;

        //a few functions
}

What i have observed is, using the this i have overridden the ObjectOutputStream and used it while i am appending the objects to the file. But what happens is if i write:

PPBusinessAccount first, repeat any number of times… then write PPAccount all is well.
PPAccount first, repeat…. then write PPBusinessAccount then write PPAccount, it writes well but while reading i get a ClassCastException.

I am tried reading the Objects and storing them directly in an instance of Object class to avoid the class cast but still readObject() throws ClassCastException.

I tried best to describe my scenario, tell if you don’t get anything. Why is this happening?? has it got something to do with the header that it is writing for this first time?? Along the lines of Base class header cannot support child class?? What’s the turn around?

I am doing the cast like this:

Object o = ois.readObject();        //Surprisingly exception is raised here (line:50 in DataStore)
PPAccount ppa = (PPAccount)o;

The stack trace

java.lang.ClassCastException: java.lang.String cannot be cast to java.io.ObjectStreamClass
    at java.io.ObjectInputStream.readClassDesc(Unknown Source)
    at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
    at java.io.ObjectInputStream.readClassDesc(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at java.util.ArrayList.readObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
    at java.io.ObjectInputStream.readSerialData(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
    at java.io.ObjectInputStream.readSerialData(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at in.msitprogram.iiit.paypal.persistance.DataStore.lookupAccount(DataStore.java:50)
    at in.msitprogram.iiit.paypal.persistance.DataStore.writeAccount(DataStore.java:131)
    at in.msitprogram.iiit.paypal.console.PPNewAccountScreen.show(PPNewAccountScreen.java:78)
    at in.msitprogram.iiit.paypal.console.MainMenu.show(MainMenu.java:42)
    at in.msitprogram.iiit.paypal.PPSystem.main(PPSystem.java:17)
Exception in thread "main" java.lang.NullPointerException
    at in.msitprogram.iiit.paypal.persistance.DataStore.lookupAccount(DataStore.java:66)
    at in.msitprogram.iiit.paypal.persistance.DataStore.writeAccount(DataStore.java:131)
    at in.msitprogram.iiit.paypal.console.PPNewAccountScreen.show(PPNewAccountScreen.java:78)
    at in.msitprogram.iiit.paypal.console.MainMenu.show(MainMenu.java:42)
    at in.msitprogram.iiit.paypal.PPSystem.main(PPSystem.java:17)

The lookUpAccount reads from the stream while writeAccount writes to the stream, here is the code:

public static PPAccount lookupAccount(String email) throws IOException, ClassNotFoundException 
    {
        PPAccount account = null; //initialize it after reading from file
        // write code to open the files, read
        PPAccount foundAccount=null;
        ObjectInputStream ois=null;
        FileInputStream fis=null;
        File ff = new File(PPConstants.AllAccountDetails);
        if(!ff.exists())
        {
            //System.out.println("Required file not found");
            return null;
        }
        try
        {
            fis=new FileInputStream(PPConstants.AllAccountDetails);
            ois = new ObjectInputStream(fis);
            while(fis.available()>0 && foundAccount==null)
            {
                //Object o=null;
                PPAccount ppa=null;
                try
                {
                    ppa = (PPAccount)ois.readObject();
                    if(ppa==null)
                        return null;
                    System.out.println(ppa);
                }

                catch(ClassCastException cce)
                {
                    System.out.println("Class cast exception "+cce.getCause());
                    cce.printStackTrace();  
                }
                if(email.equals(ppa.getEmail()))
                {
                    foundAccount=ppa;
                    break;
                }
                if(ppa instanceof PPBusinessAccount)
                {
                    PPBusinessAccount ppba = (PPBusinessAccount)ppa;
                    ArrayList<PPRestrictedAccount> alist=ppba.getAccountOperators();
                    if(alist==null)
                        continue;
                    Iterator<PPRestrictedAccount> it = alist.iterator();
                    while(it.hasNext())
                    {
                        PPRestrictedAccount ppr=(PPRestrictedAccount) it.next();
                        System.out.println(ppr);
                        if(email.equals(ppr.getEmail()))
                        {
                            foundAccount = ppr;
                            break;
                        }
                    }//iterators while loop
                }//if it is a businessAccount
            }//outer while  
        }//try
        finally
        {
            if(ois!=null)
                ois.close();
            if(fis!=null)
                fis.close();
        }   
        return foundAccount;
    }
    public static void writeAccount(PPAccount account,Boolean append) throws IOException, ClassNotFoundException, DuplicateAccountException
    {
        ObjectOutputStream oos=null;
        FileOutputStream fos=null;
        try
        {
            if(!append)
            {
                fos= new FileOutputStream(PPConstants.AllAccountDetails);
                oos = new ObjectOutputStream(fos);
                //System.out.println("Not Appending");
                oos.writeObject(account);
            }
            else
            {
                File ff = new File(PPConstants.AllAccountDetails);
                if(!ff.exists())
                {
                    System.out.println("Required file not found");
                    return;
                }
                PPAccount aa=lookupAccount(account.getEmail());
                if(aa!=null)
                    throw new DuplicateAccountException("An Account already exits with this email-ID");
                oos = new AppendingObjectOutputStream(new FileOutputStream(PPConstants.AllAccountDetails,append));
                oos.writeObject(account);
            }
        }
        finally
        {
            if(oos!=null)
                oos.close();
            if(fos!=null)
                fos.close();
        }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:06:38+00:00Added an answer on June 11, 2026 at 11:06 am

    The problem here is that the previous poster who gave you an appendable ObjectOutputStream led you astray. An ObjectOutputStream/ObjectInputStream tries to store each object only once, and then later refer back to the object already stored. That is, in the stream you can end up with something like this if you have a bunch of objects of the same class:

    CLASS_1_DESCRIPTION
    OBJECT_1
    REF_TO_CLASS_1
    OBJECT_2
    REF_TO_CLASS_1
    OBJECT_3
    ...
    

    When an ObjectInputStream is converting the stream back into a bunch of objects, it maintains a list of what things it’s already deserialized. The error it’s telling you is that it was trying to deserialize an object, read what should have been a reference to the description of the object’s class, but when it looked up that reference in its internal table it saw a String. Quite naturally, it blew up.

    I think the fix is as simple as this – in your AppendableObjectOutputStream, change this method:

      @Override
      protected void writeStreamHeader() throws IOException {
        // do not write a header, but reset the handle list
        reset();
      }
    

    the reset() method in ObjectOutputStream inserts a marker into the stream saying “throw away all the state at this point”. Then, when you read this back in with a ObjectInputStream, the input stream’s idea of what’s been deserialized will match what the output stream thought the state was when it deserialized the stuff in the first place.

    (EDIT: Answer question from comments)

    The only detrimental consequences of this that I can think of:

    • The final file will be longer than it would have been if you’d written everything all to one ObjectOutputStream, especially if the same Profile object appears multiple times. Even if not, you’ll repeat class descriptors in the stream, so lots of repetitions of {open AppendableObjectOutputStream, write one object, close stream} could bloat the file size a bit.

    • Related to that, after you deserialize everything you may end up with multiple copies of what should have been the identical object. For example, suppose you write a bunch of things including some PPRestrictedAccount objects, then close the stream, open it as an AppendableObjectOutputStream, and write out a PPBusinessAccount that has in its operators list some of the PPRestrictedAccounts you wrote out earlier. When you read all that back in, the PPRestrictedAccounts you read initially won’t be the same objects (that is, they won’t be ==) to the PPRestrictedAccounts that you find in the PPBusinessAccount‘s operators list. They’ll be instantiated separately. To avoid this, you’d need to de-duplicate them with a readResolve method. Everything that was written to a single AppendableObjectOutputStream instance will be connected correctly however. Depending on your application, this might not be something to worry about at all.

    In terms of will-this-blow-up-or-not safety, this is as safe as any other use of java serialization; there isn’t anything specific about your classes that makes it work. Just be aware that any object written in multiple separate openings of the output file will be deserialized as separate copies of the original object. (Absent any readResolve magic)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to make this to be a little jQuery plugin that
I have been trying to figure this little section of my larger project out
I am working on a little WinForm app and have been trying to find
So I am a little confused, I have been looking around trying to determine
Have been trying to encrypt an xml file to a string so that I
We have been trying to get git-subtree working on a project (with git version
I've been trying to get WCF security working for my project, and have had
I've been working on a little project and been trying to make my own
Been trying to figure this one out all day. I have a large text
I have been dropped into a sharepoint 2007 project, and i have relatively little

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.