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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:37:01+00:00 2026-06-15T20:37:01+00:00

I am trying to seriliaze an Object having a reference to another Object as

  • 0

I am trying to seriliaze an Object having a reference to another Object as instance variable. I follow the common way, nevertheless I cannot retrive in the main() method the second Object (which is non Serializable). Here is my Code:

 public class Car {

String type;
int speed;

public Car(String s, int v){
    type = s;
    speed = v;
}

}

public class Employee implements Serializable {

String name;
int Id;
transient Car car;

public Employee(String s, int i, Car c){
    name = s;
    Id = i;
    car = c;
}

private void writeObject(ObjectOutputStream os){
    try {
        os.defaultWriteObject();
        os.writeObject(car);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void readObject(ObjectInputStream is){
    try {
        is.defaultReadObject();
        car = (Car)is.readObject();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

In the main method:

public static void main(String[] args) {

    Car c = new Car("noType", 100);
    Employee e = new Employee("Aris", 1, c);

    try {
        FileOutputStream fo = new FileOutputStream("save.txt");
        ObjectOutputStream out = new ObjectOutputStream(fo);
        out.writeObject(e);
        out.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }


    try {
        FileInputStream fi = new FileInputStream("save.txt");
        ObjectInputStream in = new ObjectInputStream(fi);
        Employee emp = (Employee) in.readObject();     // Comment
        System.out.println(emp.car.speed);
        in.close();
    }catch (ClassNotFoundException e1) {
        e1.printStackTrace();
    } 
    catch (IOException e1) {
        e1.printStackTrace();
    }
}

On the line “Comment” in the second try block it throws a nullPointerException. It cannot save the Car object this way. What should I do to get over it? Ofcource I want necessarily the Car class to remain non-seriliazable. If prefer to save the instrance variables of the Car object and to recreate it by the aid of them and its constructor, how can I save (and retrieve) the String attribute?

UPDATE:

When trying to save only the int instance variable, it works. The relevant methods are this time:

private void writeObject(ObjectOutputStream os){
    try {
        os.defaultWriteObject();
        os.writeInt(car.speed);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void readObject(ObjectInputStream is){
    try {
        is.defaultReadObject();
        car = new Car("theNew", is.readInt());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

But my question is still how (or if) I can save the entire object. After all I did not manage to save and retrieve the String. WriteString(), ReadString() methods
do not exist.

  • 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-15T20:37:02+00:00Added an answer on June 15, 2026 at 8:37 pm

    The line

    car = (Car)is.readObject();
    

    is wrong.
    Car is not Serializable.

    Solutions:

    1. make car serializable, and remove the line

    2. Use a MyCar object which is Seriazable only for the purpose of ser/ deser, and create car from MyCar, see point 3.

    3. create a new Car : car = new Car(); but where to get the data from? (see point 2)

    4. seialize the fields of car, one by one using os.writeObject() and for primitives e.g os.writeInt();

    5. use your own custom serialization, using DataOutputStream

    Update answer to your update question

    String is an object, you serialize it with os.writeObject(car.type);
    See also ObjectOutputStream
    Code using solution 4:

    private void writeObject(ObjectOutputStream os){
        try {
            os.defaultWriteObject();
            os.writeObject(car.type);
            os.writeInt(car.speed);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    private void readObject(ObjectInputStream is){
        try {
            is.defaultReadObject();
            String type = (String) is.readObject();
            int speed = is.readInt();
            this.car = new Car(type, speed);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out a way to serialize some Django model object to
i am trying to XML serialize a class object but having the following problem:
I'm trying to serialize an object to meet another systems requirements. It need to
I'm having an strange error when trying to save an object into isolated storage.
I'm trying to serialize an object to memory, pass it to another process as
I am trying to serialize object having the following field: private TreeSet<TimeSlot<T>> counterTimeSlotSet =
I'm trying to serialize an object to XML that has a number of properties,
I'm trying to Serialize an object to a Byte array, for storage in a
I've run into some problems when trying to serialize my object to XML. The
I am trying to serialize a PagedList object ( https://github.com/martijnboland/MvcPaging/blob/master/src/MvcPaging/PagedList.cs ) to Json, like

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.