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

时间:2016-04-20 21:48:25

标签: javafx save load ioexception savefiledialog

我正在尝试保存到文件,但无论何时我都会收到此错误:

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类的代码在这里:

包模型;

import java.io.Serializable; import java.util.Objects;

public class Module实现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);
}

}

有没有办法让对象Serializable?

2 个答案:

答案 0 :(得分:0)

您的班级必须实施界面Serializable。它可能是这样的:

public class Module implements Comparable, Serializable{

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

答案 1 :(得分:0)

只需Module实施Serializable

public class Module implements Comparable<Module>, Serializable {

}