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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:35:55+00:00 2026-06-04T12:35:55+00:00

I am making an android app where I have a bidirectional cyclic object tree

  • 0

I am making an android app where I have a bidirectional cyclic object tree that I need to serialize to fill in a blob field in a Sqlite database.
But the default serialization implementation throws a StackOverflowError at me (stack trace at the bottom). But I have no experience in writing custom java serialization code to avoid this situation.

I this thread( StackOverflowError when serializing an object in Java ) he talks about the issue, but how to implement it in my case I haven’t really any idea:

It’s possible to define a writeObject() method for such a list class
that, when the first link is serialized, simply walks the list and
serializes each link iteratively; this will prevent the default
recursive mechanism from being used.

This is my object model(look at the 1/M, not the other signs):
enter image description here

This is the method I use to serialize the object model to a byte array (properly not that important):

public byte[] serialize(Object object) {
        byte[] buf = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutput out = new ObjectOutputStream(bos);
            out.writeObject(object);
            out.close();
            buf = bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buf;
    }

Custom serialization functions:

private void writeObject(java.io.ObjectOutputStream out) throws IOException {
    //please help me with this code
}
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
    //please help me with this code
}

Hope someone help me getting this issue solved. Any clue is appreciated.

Exception stack trace:

05-25 20:28:16.231: E/AndroidRuntime(14451): FATAL EXCEPTION: main
05-25 20:28:16.231: E/AndroidRuntime(14451): java.lang.StackOverflowError
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.util.IdentityHashMap.findIndex(IdentityHashMap.java:419)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.util.IdentityHashMap.get(IdentityHashMap.java:371)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.dumpCycle(ObjectOutputStream.java:471)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1739)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1143)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:413)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1241)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1143)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:413)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1241)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.util.ArrayList.writeObject(ArrayList.java:651)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.lang.reflect.Method.invokeNative(Native Method)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.lang.reflect.Method.invoke(Method.java:507)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1219)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1143)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:413)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1241)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1143)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:413)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1241)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.util.ArrayList.writeObject(ArrayList.java:651)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.lang.reflect.Method.invokeNative(Native Method)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.lang.reflect.Method.invoke(Method.java:507)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1219)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1143)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:413)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1241)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
05-25 20:28:16.231: E/AndroidRuntime(14451):    at java.io.ObjectOutputStream.writeObject
  • 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-04T12:35:57+00:00Added an answer on June 4, 2026 at 12:35 pm

    Java serialization handles cyclic graphs correctly, but long lists are a problem.

    If I understand correctly, your problem is a bit different than the one in the article you linked, cause that article talks about linked lists without a proper writeObject .. while currently you are using ArrayLists, which already stores objects as a flat array, and also have a proper writeObject.

    However, if I got it right, what happens is more or less this :

    You start serializing object A, during that serialization it encounters a long list, takes the first element and serializes object B, object B also has a long list, it takes the first element, say it’s A again, since it is already being serialized it skips A, so it does not end up in an endless loop, however then goes to the second object in the list of B, which is C, also C has a long list, the first two elements are A and B again, so they are skipped, the third is D, which also has a list …. and soooo on.

    Since each one of these steps are a few lines in the stack stace, it fills up, even if they are all ArrayLists with a proper writeObject method.

    This can be the case if those lists scale into relatively “big” numbers, compared to the runtime you have. That’s why I asked in the comment. Maybe a solution can be found avoiding to serialize some lists (making them transient), and rebuilding them after the “partial” deserialization.

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

Sidebar

Related Questions

I'm making an Android app and have a strange problem. I need a ViewFlipper
I'm making an android app - where I need to have weather-information. I've found
I am making an android app which will have two services that will keep
When making an Android app, I want to have the paper peel effect that's
I have recently started making an Android app that requires a list of 20
So I'm making an android app that has more than 100 buttons,but you know
I'm making a Android app that basically plays video from a website on the
I am making an android app that uses a webview to visit a website.
I am making an android game that should display a message that you have
I'm making an Android app that tracks a user and displays their location in

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.