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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:15:15+00:00 2026-05-31T11:15:15+00:00

Here is my java code: FileOutputStream fos = null; ObjectOutputStream out = null; try

  • 0

Here is my java code:

    FileOutputStream fos = null; 
    ObjectOutputStream out = null;
    try {
        fos = new FileOutputStream(pathName);
        out = new ObjectOutputStream(fos);
        out.writeObject(index);
        out.close();
        fos.close();
    } catch (IOException e) {
        LogManager.writeLogToFile(e.getMessage());
        e.printStackTrace();
    }

The index is an instance of inverted document index by myself. I have built the object index in memory, but when I pass it to the method writeObject(index), I get the error StackOverflowError. My question is: Why does this error happen after that I have built the object in the memory?

Suppose that the data structure of index is as follow:

class InvertedIndex{
    private HashMap<String, PostList> index;
}
class PostList{
    private int size;
    private PostNode first;
}
class PostNode{
    private String key;
    private double weight;
    private PostNode next;
}

To avoid recursive call, I overwrite the method writeObject and readObject of the PostList because it may be a very long list:

class PostList{
     private void writeObject(ObjectOutputStream out) throws IOException{
            PostNode temp = first;
            while(temp != null){
                  out.writeObject(temp);
                  temp = temp.getNext();
            }
     }
     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
            in.defaultReadObject();
    }
}

Then I get two questions:

  • out.writeObject(temp); Because temp has a reference to the next node, when I write temp, recursive call will also happen. The same situation will continue even if I overwrite the writeObject method of PostNode with defaultWriteObject. Am I right? If so, what can I do to avoid this recursive call?
  • How to overwrite the method readObject()?
  • 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-05-31T11:15:16+00:00Added an answer on May 31, 2026 at 11:15 am

    It’s hard to know without seeing the class you’re serializing, but my guess would be that the object graph is too complex. Java’s default serialization mechanism works recursively through the object graph; that is, if you have object A with a reference to B, and you serialize A, it’ll have to walk to B and serialize that. If B then references C, and C references D, and so on, then it’s possible for the recursive algorithm to have to go so deep that it causes a stack overflow.

    If that’s what’s causing your problem, you should write your own readObject and writeObject which avoid this recursive building, if possible.

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

Sidebar

Related Questions

Usually, when dealing with Java IO code, here is what I wrote FileOutputStream out
I happen to come across a Java code at my work place. Here's the
here is the code (java): class prime { public static boolean prime (int a,
Here is a simple piece of code: import java.io.*; public class Read { public
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame
// Here's my code: Main Class: import java.awt.*; import java.awt.image.BufferedImage; import java.awt.image.DataBufferInt; import java.awt.image.BufferStrategy;
Here is the piece of code that I have used for Java 5.0 TreeSet<Integer>
Here is my code: import javax.swing.*; import java.awt.*; public class PanelModel { public static
Here is the code: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class TestGrid {
Here is the server code package echoserver; import java.net.*; import java.io.*; public class EchoServer

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.