2016-04-20 37 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

相關問題