Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31562 > unrolled thread

NotSerializableException

Started byakkshaykhoslaa@gmail.com
First post2015-04-13 18:51 -0700
Last post2015-04-14 08:13 -0400
Articles 3 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  NotSerializableException akkshaykhoslaa@gmail.com - 2015-04-13 18:51 -0700
    Re: NotSerializableException Robert Klemme <shortcutter@googlemail.com> - 2015-04-14 07:50 +0200
    Re: NotSerializableException Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-04-14 08:13 -0400

#31562 — NotSerializableException

Fromakkshaykhoslaa@gmail.com
Date2015-04-13 18:51 -0700
SubjectNotSerializableException
Message-ID<631073ff-d7fa-411a-a003-567349a37761@googlegroups.com>
Hi,

My code is as follows:

        File ser = new File("locationinstringform");
        FileInputStream fileIn3 = new FileInputStream(ser);
        ObjectInputStream objectIn3 = new ObjectInputStream(fileIn3);
        myObj = (MyObj) objectIn3.readObject();
        objectIn3.close();
        fileIn3.close();
        myObj.someFunction(x, y);
        FileOutputStream fileOut3 = new FileOutputStream(ser);
        ObjectOutputStream objectOut3 = new ObjectOutputStream(fileOut3);
        objectOut3.writeObject(myObj);
        objectOut3.close();
        fileOut3.close();

When I run my code, I get an error tracing to the line that says objectOut3.writeObject(myObj);

The error is as follows: 
Exception in thread "main" java.io.NotSerializableException: java.util.HashMap$KeySet

However, that MyObj class I have defined isn't a HashMap or Set, and doesn't have a HashMap or Set. It only has 2 instance objects, one of which is one is an object I coded, and the other is an ArrayList. I have made sure all classes I have coded implement the serializable interface. 

Any ideas as to why the problem could be arising and what the solution is? I have been stuck for a long time so any help would be much appreciated.

[toc] | [next] | [standalone]


#31563

FromRobert Klemme <shortcutter@googlemail.com>
Date2015-04-14 07:50 +0200
Message-ID<cp3o5uF7i2tU1@mid.individual.net>
In reply to#31562
On 14.04.2015 03:51, akkshaykhoslaa@gmail.com wrote:
> Hi,
>
> My code is as follows:
>
>          File ser = new File("locationinstringform");
>          FileInputStream fileIn3 = new FileInputStream(ser);
>          ObjectInputStream objectIn3 = new ObjectInputStream(fileIn3);
>          myObj = (MyObj) objectIn3.readObject();
>          objectIn3.close();
>          fileIn3.close();
>          myObj.someFunction(x, y);
>          FileOutputStream fileOut3 = new FileOutputStream(ser);
>          ObjectOutputStream objectOut3 = new ObjectOutputStream(fileOut3);
>          objectOut3.writeObject(myObj);
>          objectOut3.close();
>          fileOut3.close();
>
> When I run my code, I get an error tracing to the line that says objectOut3.writeObject(myObj);
>
> The error is as follows:
> Exception in thread "main" java.io.NotSerializableException: java.util.HashMap$KeySet
>
> However, that MyObj class I have defined isn't a HashMap or Set, and
> doesn't have a HashMap or Set. It only has 2 instance objects, one of
> which is one is an object I coded, and the other is an ArrayList. I
> have made sure all classes I have coded implement the serializable
> interface.

Obviously that assumption must be false: there must be a HashMap or sub 
class of it somewhere in the graph of objects you are trying to 
serialize.  Since you seem to read it from a serialized stream 
candidates to look closer are someFunction() as well as readResolve(), 
readObject() and readObjectNoData() and their write counterparts.  See

http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html

> Any ideas as to why the problem could be arising and what the
> solution is? I have been stuck for a long time so any help would be
> much appreciated.

Without seeing class MyObj and probably also super and sub classes it's 
all only guessing.

Cheers

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#31565

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-04-14 08:13 -0400
Message-ID<mgj073$od6$1@dont-email.me>
In reply to#31562
On 4/13/2015 9:51 PM, akkshaykhoslaa@gmail.com wrote:
> Hi,
>
> My code is as follows:
>
>          File ser = new File("locationinstringform");
>          FileInputStream fileIn3 = new FileInputStream(ser);
>          ObjectInputStream objectIn3 = new ObjectInputStream(fileIn3);
>          myObj = (MyObj) objectIn3.readObject();
>          objectIn3.close();
>          fileIn3.close();
>          myObj.someFunction(x, y);
>          FileOutputStream fileOut3 = new FileOutputStream(ser);
>          ObjectOutputStream objectOut3 = new ObjectOutputStream(fileOut3);
>          objectOut3.writeObject(myObj);
>          objectOut3.close();
>          fileOut3.close();
>
> When I run my code, I get an error tracing to the line that says objectOut3.writeObject(myObj);
>
> The error is as follows:
> Exception in thread "main" java.io.NotSerializableException: java.util.HashMap$KeySet
>
> However, that MyObj class I have defined isn't a HashMap or Set, and doesn't have a HashMap or Set. It only has 2 instance objects, one of which is one is an object I coded, and the other is an ArrayList. I have made sure all classes I have coded implement the serializable interface.
>
> Any ideas as to why the problem could be arising and what the solution is? I have been stuck for a long time so any help would be much appreciated.

     Your object refers -- directly or indirectly -- to a HashMap.KeySet
instance.  You mention an ArrayList: What does that list contain?  Does
it, perhaps, hold a Set that turns out to be a HashMap.KeySet?  Or does
it hold a Thing that refers to a Widget that refers to a Whatnot that
refers to a Gizmo that refers to a HashMap.KeySet?

     Serializing an object also serializes everything the object refers
to, and everything those objects refer to, and so on through the entire
object graph.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web