Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news2.arglkargh.de!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: NotSerializableException Date: Tue, 14 Apr 2015 08:13:13 -0400 Organization: A noiseless patient Spider Lines: 40 Message-ID: References: <631073ff-d7fa-411a-a003-567349a37761@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 14 Apr 2015 12:12:19 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="858a9d65d95f4ea4d355bd116aa2cdbf"; logging-data="24998"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/imoMbJwIcNsJqJ2k75Zgw" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: <631073ff-d7fa-411a-a003-567349a37761@googlegroups.com> Cancel-Lock: sha1:npu22ZTjhBs0g0liiO5y1PQhLPY= Xref: csiph.com comp.lang.java.programmer:31565 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