We help IT Professionals succeed at work.

Java write hashmap to disk containing custom class NotSerializableException

va3jsl
va3jsl asked
on
330 Views
Last Modified: 2012-02-10
Why do I get NotSerializableException with this:

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.io.Serializable;

public class Tester {
	
	private HashMap map = new HashMap();
	
	public Tester(){
		try {
			myObject o = new myObject("AA", "BB");
			map.put("xxx", o);
			
                                                File file = new File("test.xxx");   
			FileOutputStream f = new FileOutputStream(file);   
			ObjectOutputStream s = new ObjectOutputStream(f);           
			s.writeObject(map); 
			s.flush(); 
		} 
		catch(Exception e) {
			e.printStackTrace();
		} 

	}
	
	private class myObject implements Serializable{
		
		private String name;
		private String value;
		
		public myObject(String name, String value){
			this.name = name;
			this.value = value;
		}
	}
}

Open in new window

Comment
Watch Question

Awarded 2011
Awarded 2011
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks for prompt and well presented response.  Had to read it a few times but now get the concept.  Its a good day when I learn something new.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.