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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:29:54+00:00 2026-06-01T19:29:54+00:00

It is going to be a very basic question I suppose, but I’ve been

  • 0

It is going to be a very basic question I suppose, but I’ve been searching for the answer fo hours and I just can’t figure out where’s my code go wrong. So: I do serialze an object called SerializableObject, than read it back. In the deserializing method I get a temporary object, which I want to copy to an other new SerializeableObject, wich I want to use after, but I can’t copy it properly, however the temporary object got the values properly, at the deserialization. Here’s my classes:
SeralizeableObject:

public class SerializableObject implements Serializable, Cloneable{

private Vector<int[]> mixMade;
private Vector<int[]> stepsMade;
private long time;
private int steps;
private int winnerState;

public SerializableObject(Vector<int[]> mixMade, Vector<int[]> stepsMade,
 long time, int steps, int winnerState) {
    this.mixMade = mixMade;
    this.stepsMade = stepsMade;
    this.time = time;
    this.steps = steps;
    this.winnerState = winnerState;
}

@Override
public String toString() {
    String str = "";
    for(int[] mixEl : mixMade){
        str += mixEl[0] + ", " + mixEl[1] + "|";
    }
    str += " mixes\n";
    for(int[] stepEl : stepsMade){
        str += stepEl[0] + ", " + stepEl[1] + "|";
    }
    str += " steps\n";
    str += "time: " + time + ", stepsnum: " + steps + ", 
winstate: " + winnerState;
    return str;
}    

@Override
public SerializableObject clone() {
    SerializableObject serObj;
    Vector<int[]> mixMadeTemp   = new Vector<int[]>();
    Vector<int[]> stepsMadeTemp   = new Vector<int[]>();
    for(int i = 0; i < mixMade.size(); ++i){
        mixMadeTemp.add(mixMade.get(i));
    }
    for(int i = 0; i < stepsMade.size(); ++i){
        stepsMadeTemp.add(stepsMade.get(i));
    }
    serObj = new SerializableObject(mixMadeTemp, stepsMadeTemp, 
time, steps, winnerstate);
    return serObj;
}

}

The Serializator:

public class ObjectSerializator {

public ObjectSerializator() {

}

public void toFile(String filepath, SerializableObject serObj){
    ObjectOutputStream out;
    try{
        FileOutputStream fileOut = new FileOutputStream(filepath);
        out = new ObjectOutputStream(fileOut);
        out.writeObject(serObj);
    }catch (IOException ex) {

    }    
}

public void fromFile(String filepath, SerializableObject serObj){
    SerializableObject tempSerObj;
    try {
        FileInputStream fileIn = new FileInputStream(filepath);
        ObjectInputStream in = new ObjectInputStream(fileIn);

        tempSerObj = (SerializableObject) in.readObject();
        System.out.println(tempSerObj + "TEMP");
        serObj = tempSerObj.clone();

        in.close();
        fileIn.close();
    } catch (IOException ex) {

    } catch (ClassNotFoundException ex) {

    }
}

}

note: the serobj reference points properly to the good values, inside the fromFile method
A filechooser class, where the user can chhose the file to load from:

public class FileChooser extends JFileChooser{

private ObjectSerializator serializator;

public FileChooser() {
    serializator = new ObjectSerializator();
}

public void load(SerializableObject serObj){
        int retValue = showOpenDialog(null);
        serializator.fromFile(getSelectedFile().getAbsolutePath(), serObj);
}}

than in my mainfram I call it from a buttonaction

public void load(){
    SerializableObject serObj = new SerializableObject(new Vector<int[]>(), new 
Vector<int[]>(), 10, 10, 200);
    fileChooser.load(serObj);
    System.out.println(serObj + " LAST");
}

Here’s my output:

3, 2|4, 3| mixes

0, 0|0, 1|0, 0| steps

time: 6000, stepsnum: 3, winstate: 0TEMP

-------------------

 mixes

 steps

time: 10, stepsnum: 10, winstate: 200 LAST
  • 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-01T19:29:55+00:00Added an answer on June 1, 2026 at 7:29 pm

    The object you’ve deserialized gets lost.

    In your fromFile method, you have the following line:

    serObj = tempSerObj.clone();
    

    But serObj is an argument to the method. The line above changes the local serObj variable, whereas the original object remains intact. The calling method (load) still holds a reference to the old (original) object.

    Your fromFile method shouldn’t accept a SerializableObject as an argument; rather, it should return one. Then the FileChooser.load method should return it as well. Finally, the load method of the main frame should look something like:

    public void load() {
        SerializableObject originalObject = new SerializableObject(new Vector<int[]>(), new Vector<int[]>(), 10, 10, 200);
        // Here you can do something with the newly created object, such as save it to a file.
    
        SerializableObject deserializedObject = fileChooser.load();
        System.out.println(deserializedObject + " LAST");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a very basic regex question, but I'm not quite sure where I
I know this is a very basic question, thats why I just want a
A very basic question. I have just installed Visual Studio 2008 Express Edition and
Maybe my question is very basic, but regretfully I have very little experience with
This may be a very basic question but somehow it got me tricked... when
I am going to ask a VERY general question. I was working on a
I'm going to implement license key generator on my website but I've very few
I think this is a very simple question, but I’m new to programming so
I’ve just build my first Helper in MVC, it’s very basic and just displays
First, this is a very basic question that I'm unsure of how to phrase.

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.