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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:00:56+00:00 2026-06-11T21:00:56+00:00

I want to serialize a class using ByteArrayOutputStream of java: public void serializ(UniqueTag m)

  • 0

I want to serialize a class using ByteArrayOutputStream of java:

public void serializ(UniqueTag m) throws IOException {
        ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
        ObjectOutputStream stream = new ObjectOutputStream(byteOutput);
            stream.writeObject(m);

        stream.flush();
        stream.close();
        byteOutput.flush();
        byteOutput.close();    
    }

A class want to serialize :

import java.io.Serializable;

public class UniqueTag implements Comparable<UniqueTag>, Serializable {
    public final static UniqueTag MIN = new UniqueTag(Integer.MIN_VALUE,
            Integer.MIN_VALUE);
    public final static UniqueTag MAX = new UniqueTag(Integer.MAX_VALUE,
            Integer.MAX_VALUE);

    private final int replicaId;
    private final int counter;

    public static UniqueTagGenerator createGenerator() {
        return new UniqueTagGenerator() {
            int currentStamp;

            @Override
            public UniqueTag nextTag(final int replicaId) {
                return new UniqueTag(replicaId, currentStamp++);
            }
        };
    }

    public UniqueTag(final int replicaId, final int counter) {
        this.replicaId = replicaId;
        this.counter = counter;
    }

    public int getReplicaId() {
        return replicaId;
    }

    public int getCounter() {
        return counter;
    }

    @Override
    public int compareTo(UniqueTag o) {
        if (replicaId != o.replicaId) {
            return replicaId - o.replicaId;
        }
        return counter - o.counter;
    }

    public UniqueTag clone() {
        return new UniqueTag(replicaId, counter);
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof UniqueTag))
            return false;
        final UniqueTag other = (UniqueTag) obj;
        return counter == other.counter && replicaId == other.replicaId;
    }

    @Override
    public String toString() {
        return "(" + replicaId + "," + counter + ")";
    }
}

I have this error :

Exception in thread "main" java.io.NotSerializableException: jbenchmarker.treedoc.UniqueTag$1
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)

Please where is a problem ? i serialize a class but it displays to me an error ! 🙁

  • 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-11T21:00:57+00:00Added an answer on June 11, 2026 at 9:00 pm

    It’s complaining that an anonymous inner class is causing the problem, and you only have one anonymous inner class, so I believe that you can solve the problem by making UniqueTagGenerator implement Serializable.

    I’m not entirely certain why this exception is being thrown, as an instance of UniqueTag should not hold any reference to the generator that created it. I suspect there’s some needless chasing of class references going on (or you aren’t showing the exact code).

    Another way to fix it — better, IMO — is to separate out the tag generator implementation from UniqueTag. Either create a separate class, or add the factory method to UniqueTagGenerator.

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

Sidebar

Related Questions

I have a Joo.java class and I want to serialize it into XML using
I want to serialize this class: public class CarDisplay { public string Name {
I want to serialize a class to xml and store that in a field
i want to serialize this class, to store it on the sdcard of the
I have a super class and two subclasses and I want to serialize the
I have a class in .NET that implements IXmlSerializable. I want to serialize its
I want to serialize a 'Player' class and send it over my network stream
I have a class which contains a list of items. I want to serialize
I'm using DataContractSerializer to serialize a class with DataContract and DataMember attributes to an
I want to serialize an object. I've got this basic class structure: class Controller

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.