由于一个对象不可序列化,无法保存或加载文件

Posted

技术标签:

【中文标题】由于一个对象不可序列化,无法保存或加载文件【英文标题】:Cannot save or load file due to one object not being Serializable 【发布时间】:2016-04-20 21:48:24 【问题描述】:

我正在尝试保存到文件,但每当我这样做时,我都会收到此错误:

java.io.WriteAbortedException: writing aborted; `enter code here`java.io.NotSerializableException: Model.Module
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at java.util.TreeMap.buildFromSorted(Unknown Source)
    at java.util.TreeMap.buildFromSorted(Unknown Source)
    at java.util.TreeMap.buildFromSorted(Unknown Source)
    at java.util.TreeMap.buildFromSorted(Unknown Source)
    at java.util.TreeMap.readTreeSet(Unknown Source)
    at java.util.TreeSet.readObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
    at java.io.ObjectInputStream.readSerialData(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
    at java.io.ObjectInputStream.readSerialData(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at Controller.MCController$LoadMenuHandler.handle(MCController.java:265)
    at Controller.MCController$LoadMenuHandler.handle(MCController.java:1)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.control.MenuItem.fire(Unknown Source)
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(Unknown Source)
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
    at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(Unknown Source)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.notifyMouse(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.io.NotSerializableException: Model.Module
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at java.util.TreeSet.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at Controller.MCController$SaveMenuHandler.handle(MCController.java:291)
    at Controller.MCController$SaveMenuHandler.handle(MCController.java:1)
    ... 42 more

Module 类的代码在这里:

封装模型;

导入 java.io.Serializable; 导入 java.util.Objects;

公共类模块实现 Comparable

private String moduleCode;
private String moduleName;
private int credits;
private boolean mandatory;

public Module(String moduleCode, String moduleName) 
    this(moduleCode, moduleName, 30, false);


public Module(String moduleCode, String moduleName, int credits, boolean mandatory) 
    this.moduleCode = moduleCode;
    this.moduleName = moduleName;
    this.credits = credits;
    this.mandatory = mandatory;


public String getModuleCode() 
    return moduleCode;


public void setModuleCode(String moduleCode) 
    this.moduleCode = moduleCode;


public String getModuleName() 
    return moduleName;


public void setModuleName(String moduleName) 
    this.moduleName = moduleName;


public int getCredits() 
    return credits;


public void setCredits(int credits) 
    this.credits = credits;


public boolean isMandatory() 
    return mandatory;


public void setMandatory(boolean mandatory) 
    this.mandatory = mandatory;



@Override
public String toString() 

    return moduleCode + " : " + moduleName;


public String actualToString() 
    return "Module:[moduleCode=" + moduleCode + ", moduleName="
            + moduleName + ", credits=" + credits + ", mandatory=" + mandatory + "]";



@Override
public int compareTo(Module other) 
    int result = Boolean.compare(other.mandatory, this.mandatory);

    if (result == 0) 
        result = Integer.compare(this.credits, other.credits);

        if (result == 0) 
            result = this.moduleCode.compareTo(other.moduleCode);

            if (result == 0) 
                result = this.moduleName.compareTo(other.moduleName);
            

        
    

    return result;


@Override
public boolean equals(Object obj) 
    if (obj == null || obj.getClass() != this.getClass())
        return false;

    Module other = (Module) obj;

    return this.mandatory == other.mandatory && this.credits == other.credits &&
            this.moduleCode.equals(other.moduleCode) && this.moduleName.equals(other.moduleName);



@Override
public int hashCode() 
    return Objects.hash(mandatory, credits, moduleCode, moduleName);

有什么方法可以使对象可序列化?

【问题讨论】:

【参考方案1】:

你的类必须实现接口Serializable。可能是这样的:

public class Module implements Comparable, Serializable

然后实现接口的方法。这样做你一定没有问题。

【讨论】:

我真的试过了,但没有用。我刚刚又试了一次,它完全正常工作......抱歉浪费你的时间! 记得将评论标记为答案,以帮助其他有同样问题的人。【参考方案2】:

只需让Module 实现Serializable

public class Module implements Comparable<Module>, Serializable 


【讨论】:

我真的试过了,但没有用。我刚刚又试了一次,它完全正常工作......抱歉浪费你的时间!

以上是关于由于一个对象不可序列化,无法保存或加载文件的主要内容,如果未能解决你的问题,请参考以下文章

可序列化和不可序列化对象保存到数据库之间的区别

如何通过序列化处理和保存大文件?

保存文件格式

将用户数据保存在文本文件或序列化对象中

如何序列化对象并将其保存到 Android 中的文件中?

Django Rest Framework:无法序列化或保存上传的图像