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

  • Home
  • SEARCH
  • 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 162273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:21:41+00:00 2026-05-11T11:21:41+00:00

I have a node tree built out of Node objects. They are more complex

  • 0

I have a node tree built out of Node objects. They are more complex than the code I am showing but only have primitive or Serializable instance members.

Assuming each Node can have up to 10 children the code looks a bit like so.

public class Node implements Serializable{      private static final long serialVersionUID = -848926218090221003L;  private Node _parent; private boolean _hasParent; private Node[] _children; private int _childCount = 0;  public Node(Node parent){      _children = new Node[10];     _parent = parent;     _hasParent = (parent != null); }       ...      //Various accessors etc  } 

Now this tree is quite expensive to build but once it is done I write it to a file:

ObjectOutputStream serializedOuput = new ObjectOutputStream(new FileOutputStream(cacheFile)); serializedOuput.writeObject(tree); serializedOuput.close(); 

When I am done caching the tree I make some irreversable changes to like trimming off unneeded branches.

Then when I next need a base tree to work from I create a new tree object by reading in my serialized file.

The problem…

Creating the tree from the file seems to simply create a new object that points to the old one. In other words the modifications made after writing to file have also been made to the new tree.

If I restart the app and try reading in the serialized file it returns null.

So I seem to be serializing object references rather than the object itself, any ideas where I am going wrong?

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T11:21:41+00:00Added an answer on May 11, 2026 at 11:21 am

    Works for me, GNU/Linux with gij 4.3.2.

    Make sure all of your fields in the Node class are serializable as well.

    This is the code I tried (should be equivalent to yours):

    import java.io.*;  public class TestCase {     public static void main(String[] args) {         try {             if (args.length > 0 && args[0].equals('read')) {                 ObjectInputStream ois = new ObjectInputStream(                         new FileInputStream('File.log')                     );                 Node root = (Node) ois.readObject();                 ois.close();                  System.out.println('Root value: ' + root.getValue());                 for(Node n : root.children())                     System.out.println('Child: ' + n.getValue());                  return;             }              Node root = new Node(null, 'First');             root.add(                     new Node(root, 'Second.Child.1'),                     new Node(root, 'Second.Child.2')                 );              ObjectOutputStream oos = new ObjectOutputStream(                     new FileOutputStream('File.log')                 );             oos.writeObject(root);             oos.close();         } catch(Exception e) {             System.err.println(e.getMessage());             e.printStackTrace();         }     } }  class Node implements Serializable {     private static final long serialVersionUID = 2304728943L;     private Node _parent;     private Node[] _children;     private String _value;      public Node(Node parent, String value) {         this._parent = parent;         this._value = value;         this._children = new Node[2];     }     public void add(Node child, Node child2) {         this._children[0] = child;         this._children[1] = child2;     }     public String getValue() {         return this._value;     }     public Node[] children() {         return this._children;     }     public Node parent() {         return this._parent;     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 107k
  • Answers 107k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer String path = ServletContext.getRealPath("/fonts"); Javadoc. May 11, 2026 at 9:09 pm
  • Editorial Team
    Editorial Team added an answer It seems that the answer is rather dull. My problem… May 11, 2026 at 9:09 pm
  • Editorial Team
    Editorial Team added an answer You're pretty close to the correct answer already. Basically if… May 11, 2026 at 9:09 pm

Related Questions

Are there any good language-agnostic distributed make systems for linux that are secure and
Greetings, I'm looking for a method to do in-line result (output) document selecting in
I need to find a path or paths down a complicated graph structure. The
Is it possible to use BindingSource.find on multple columns? I have a treeview and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.