我的JAVA学习之异常整理-java.io.NotSerializableException

代码

public class SouSouUser implements Serializable{
	private String ID = null;					//用户卡号
	private String name = null;			//用户姓名
	private String password = null;		//用户密码
	private double balance = 0;					//用户余额
	private String type = null;				//用户套餐类型
	private SouSouType ssType = null; //嗖嗖套餐

运行结果

java.io.WriteAbortedException: writing aborted; 
java.io.NotSerializableException: hj.java.project.SouSou.SouSouType

产生原因:
被序列化的对象中存在不可序列化的对象,本题中,SouSouUser实现了Serializable,可以被序列化,但序列化时,由于SouSouUser的属性SouSouType类未实现Serializable,导致整体无法被序列化而报此异常。
解决方法:

public class SouSouType implements Serializable{

将被序列化的对象中所有成员都实现序列化

猜你喜欢

转载自blog.csdn.net/weixin_43041241/article/details/84347228