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 9045611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:23:23+00:00 2026-06-16T11:23:23+00:00

I have a custom class (constructor below) that I cannot seem to serialize. public

  • 0

I have a custom class (constructor below) that I cannot seem to serialize.

public ObjectNode(String name, int crackLevel,ArrayList<ObjectNode> filesOnComputer)

Every time I try to serialize an object of this class from an ArrayList of object nodes I get a Class cast exception. ArrayList cannot be cast to ObjectNode

Code- Global Vars:

    ArrayList<ObjectNode>a=new ArrayList<ObjectNode>();
File file = new File("/mnt/sdcard/","cheesepuff.txt");

Relevant Code:

        public void serializeFile()
{       
    a.add(new ObjectNode("Level 1 Waterwall","ww1", 1, 5));
    try { 
        Log.i("AAA","before serialize: "+a.toString());
        FileOutputStream fos = new FileOutputStream(file); 
        ObjectOutputStream oos = new ObjectOutputStream(fos); 
        oos.writeObject(a); 
        oos.flush(); 
        oos.close(); 
        Log.i("AAA","finished serialize");
        } 
        catch(Exception e) { 
        Log.i("aaa","Exception during serialization: " + e); 
        System.exit(0); 
        } 
}

public void deserializeFile()
{
    try { 
        FileInputStream fis = new FileInputStream(file); 
        ObjectInputStream ois = new ObjectInputStream(fis); 
        ObjectNode obj=(ObjectNode)ois.readObject();
        Log.i("aaa","obj: "+obj.ts());
        //a.add((ObjectNode) ois.readObject()); 
        ois.close(); 
        Log.i("aaa","after serialize: " + a); 
        } 
        catch(Exception e) { 
        Log.i("aaa","Exception during deserialization: " + 
        e); 
        System.exit(0); 
        } 
        }

Is my best option to make everything a string in order to serialize that, and then convert the string back to what I actually need after deserialization?

  • 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-16T11:23:25+00:00Added an answer on June 16, 2026 at 11:23 am

    You are serializing a which is ArrayList<ObjectNode>.

    oos.writeObject(a); 
    

    When deserializing, you get back exactly that, but you try to store it as ObjectNode

    ObjectNode obj=(ObjectNode)ois.readObject();
    

    You should do

    ArrayList<ObjectNode> obj = (ArrayList<ObjectNode>)ois.readObject();
    

    UPDATE: Or, as PeterLawrey correctly states,

    List<ObjectNode> obj = (List<ObjectNode>) ois.readObject;
    

    (probably then you would have to redefine a as List<ObjectNode>, but that is another good thing to do).

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

Sidebar

Related Questions

I have a class that takes an action in it's constructor. Example: public CustomClass(Action<Path>
I have a custom class with constructor, the class is setup such that it
I have a custom class that has as an instance variable of a coordinate:
I have a custom class that uses boost mutexes and locks like this (only
I have a custom class that I use to build table strings. I would
Bit of a noob question but: I have a custom class defined as: public
I have a custom struct : struct A { public int y; } a
I have a custom class called Rows that implements IEnumerable<Row> . I often use
I have created a custom class (the .as is below:). It exists throughout my
I have this custom class ... public class DrawView extends ImageView When I set

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.