2016-04-20 36 views
0

我想保存到文件,但每当我做我得到这个错误:不能保存或加载文件,由于一个对象没有被序列化

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 

的模块类的代码是在这里:

包型号;

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

公共类模块实现可比{

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); 
} 

}

有什么办法使对象序列化?

回答

0

您的课程必须实现接口Serializable。它可能是这样的:

public class Module implements Comparable, Serializable{ 

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

+0

其实我尝试这样做,也没有工作。我只是再试一次,它完全正常工作...对不起,浪费你的时间! – user3470107

+0

请记住将评论标记为答案,以帮助其他具有相同问题的人。 –

0

只是要Module实施Serializable

public class Module implements Comparable<Module>, Serializable { 

} 
+0

我实际上已经尝试过了,但没有奏效。我只是再试一次,它完全正常工作...对不起,浪费你的时间! – user3470107

相关问题