2015-01-04 2 views
4

객체 직렬화와 함께 작동하는 클래스를 작성했습니다. 직렬화 단계에서 예외가 발생하는 것 같습니다. 이 예외에 대해 아무 것도 찾을 수 없었습니다. 또한 HashMap의 별칭 Node를 사용하여 내부 클래스를 찾을 수 없었습니다.

[04/01/15 2:33 PM]: java.io.NotSerializableException: java.util.HashMap$Node 
[04/01/15 2:33 PM]:  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184) 
[04/01/15 2:33 PM]:  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348) 
[04/01/15 2:33 PM]:  at java.util.ArrayList.writeObject(ArrayList.java:762) 
[04/01/15 2:33 PM]:  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
[04/01/15 2:33 PM]:  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
[04/01/15 2:33 PM]:  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
[04/01/15 2:33 PM]:  at java.lang.reflect.Method.invoke(Method.java:483) 
[04/01/15 2:33 PM]:  at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988) 
[04/01/15 2:33 PM]:  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496) 
[04/01/15 2:33 PM]:  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432) 
[04/01/15 2:33 PM]:  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178) 
[04/01/15 2:33 PM]:  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548) 
[04/01/15 2:33 PM]:  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509) 
[04/01/15 2:33 PM]:  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432) 
[04/01/15 2:33 PM]:  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178) 
[04/01/15 2:33 PM]:  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348) 
[04/01/15 2:33 PM]:  at ab.server.data.ServerData.saveField(ServerData.java:157) 
[04/01/15 2:33 PM]:  at ab.server.data.ServerData.processQueue(ServerData.java:195) 
[04/01/15 2:33 PM]:  at ab.Server.lambda$0(Server.java:260) 
[04/01/15 2:33 PM]:  at ab.Server$$Lambda$15/2011997442.run(Unknown Source) 
[04/01/15 2:33 PM]:  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 
[04/01/15 2:33 PM]:  at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) 
[04/01/15 2:33 PM]:  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) 
[04/01/15 2:33 PM]:  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) 
[04/01/15 2:33 PM]:  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
[04/01/15 2:33 PM]:  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
[04/01/15 2:33 PM]:  at java.lang.Thread.run(Thread.java:745) 

이것은 직렬화되는 클래스입니다.

public class WellOfGoodWill implements Serializable { 

    private static final long serialVersionUID = -3335621107999346623L; 

    /** 
    * The total number of winners possible for each draw 
    */ 
    private static final int MAXIMUM_WINNERS = 3; 

    /** 
    * Compares the value of an entry 
    */ 
    private static final Comparator<Entry<String, Integer>> HIGHEST_VALUE = Entry.comparingByValue(); 

    /** 
    * Contains all of the entries for the week 
    */ 
    private HashMap<String, Integer> entries = new HashMap<>(); 

    /** 
    * Winners of the previous week 
    */ 
    private Collection<Entry<String, Integer>> winners; 

    /** 
    * The date the week is over. The initial date is set. 
    */ 
    private Date date = Misc.getFutureDate(2014, Calendar.DECEMBER, 28, 18, 0, 0); 

    /** 
    * Requests that an update be made on the entries variable. 
    * @param player the key being updated 
    * @param amount the amount being added 
    */ 
    public void update(String player, Integer amount) { 
     if (!entries.containsKey(player)) { 
      entries.put(player, amount); 
     } else { 
      int oldValue = entries.get(player); 
      entries.replace(player, oldValue, oldValue + amount); 
     } 
     Server.getServerData().setWellOfGoodWill(this); 
    } 

    /** 
    * Clears all entries in the map 
    */ 
    public void clear() { 
     entries.clear(); 
    } 

    /** 
    * Determines the weekly winner based on their contributions 
    * @return the winner 
    */ 
    public Collection<Entry<String, Integer>> getSortedResults() { 
     List<Entry<String, Integer>> list = new ArrayList<>(entries.entrySet()); 
     list.sort(HIGHEST_VALUE); 
     Collections.reverse(list); 
     return new ArrayList<>(list.subList(0, list.size() < MAXIMUM_WINNERS ? list.size() : MAXIMUM_WINNERS)); 
    } 

    /** 
    * The map containing all of the entries for the week 
    * @return the entries 
    */ 
    public Map<String, Integer> getEntries() { 
     return entries; 
    } 

    /** 
    * The winner of the previous week 
    * @return the winner 
    */ 
    public Collection<Entry<String, Integer>> getWinners() { 
     return winners; 
    } 

    /** 
    * Sets the new winner of the week 
    * @param winner the winner 
    */ 
    public void setWinners(Collection<Entry<String, Integer>> winners) { 
     this.winners = winners; 
    } 

    /** 
    * The date the week started 
    * @return the date 
    */ 
    public Date getDate() { 
     return date; 
    } 

    /** 
    * Sets the date of the week the event starts on 
    * @param date the date 
    */ 
    public void setDate(Date date) { 
     this.date = date; 
    } 

} 
+0

모든 기본 시리얼이 작업을 위해 일시적 또는 직렬화 중 하나를해야합니다. – Prashant

+0

Collection 및 Entry 모두 Serializable을 구현하지 않아서 클래스에 필요한 수정을가했습니다. –

+0

지금 작동합니까 ?? – Prashant

답변

0

그것은 당신이 Serializable, 그 클래스의 모든 필드는 Serializable이어야합니다로 직렬화하려고하는 클래스에 대한 충분하지 않습니다. 특히 CollectionMap.Entry을 비롯한 대부분의 인터페이스는 Serializable을 구현하지 않습니다.

그래프를 탐색 할 때 Serializable 인터페이스를 지원하지 않는 개체가 발생할 수 있습니다. 이 경우 NotSerializableException이 발생하고 직렬화 불가능 객체의 클래스를 식별합니다.

는 엄지 손가락의 규칙으로, 당신은 항상 그렇지 않으면 혼란 런타임 오류가 이런 종류로 실행할 수 있습니다 (대신 List 또는 Collection의, 예를 들어, ArrayList) 직렬화 가능 클래스의 필드로 명시 적으로 직렬화 형식을 사용합니다.

writeObject() 필드를 처리하거나 비 Serializable 필드를 처리하거나 transient 속성을 사용하여 serializer가 해당 필드를 무시해야 함을 나타낼 수 있습니다.

자세한 내용 : 당신의 비 정적 필드의 https://docs.oracle.com/javase/8/docs/technotes/guides/serialization/index.html

관련 문제