troubleshooting Question

Java write hashmap to disk containing custom class NotSerializableException

Avatar of va3jsl
va3jslFlag for Canada asked on
Java
2 Comments1 Solution334 ViewsLast Modified:
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;
		}
	}
}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros