java.io.NotSerializableException 通过 ObjectOutputStream.write 对象

java.io.NotSerializableException by ObjectOutputStream.write Object

我有一个对象,我想写入文件:

class PersonDaten implements Serializable {
    private static final long serialVersionUID = 1L;

    String firstname = "";
    Sting lastname = "";    
}

class InputOutput {
    public static <T> void output(T daten, String datei){
        try {
            FileOutputStream fout = new FileOutputStream(datei);
            ObjectOutputStream oos = new ObjectOutputStream(fout);
            oos.writeObject(daten);
            oos.close();
        }
        catch (Exception e) { e.printStackTrace(); }
    }
}

我有一个 Runnable,它与 Reflection 一起工作,使 SwingUtilities.invokeLater() 只有 1 次。

public class UpdateRoutine implements Runnable, Serializable {
    private static final long serialVersionUID = 1L;

    List<Map.Entry<Method, Object[]>> updates;

    public UpdateRoutine(){
        updates = new ArrayList<Map.Entry<Method, Object[]>>();
    }

    @Override
    public void run() {
        for(int i = 0; i < updates.size(); i++){
            Entry<Method, Object[]> m = updates.get(i);

            Object[] values = m.getValue();

            try {
                if(values.length == 1){
                    m.getKey().invoke(values[0]);
                }
                else if(values.length == 2){
                    m.getKey().invoke(values[0], values[1].getClass().cast(values[1]));
                }
                else if(values.length == 3){
                    m.getKey().invoke(values[0], values[1].getClass().cast(values[1]), 
                            values[2].getClass().cast(values[2]));
                }
                else if(values.length == 4){
                    m.getKey().invoke(values[0], values[1].getClass().cast(values[1]), 
                            values[2].getClass().cast(values[2]), values[3].getClass().cast(values[3]));
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                System.out.println(m.getKey() + " " + values[0]);
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }

    public void addToUpdate(Method m, Object[] args){
        Entry<Method, Object[]> se = new SimpleEntry<Method, Object[]>(m, args);

        updates.add(se);
    }
}

通过关闭我在 Main 中调用的应用程序

addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent windowEvent) {
        userDaten.save();
        System.exit(0);
    }
});

然后是异常

java.io.NotSerializableException: java.lang.reflect.Method
at util.InputOutput.output(InputOutput.java:40)
at training.Person.save(Person.java:55)
at ui.Main.windowClosing(Main.java:105)

仅当 "File.dat" 在第一个 运行 之前不存在时才会出现此异常。 如果我不通过应用程序的第一个 运行 使用 UpdateRoutine,"File.dat" 可以毫无例外地保存,然后我永远不会遇到这个问题。

  1. PersonDaten 尚未导入 java.lang.reflect.Method。为什么会出现这个异常?
  2. 如何避免这个异常?

已解决。

我有 PersonDaten 作为内部 Class。

拥有自己的 PersonDaten class 并且没有问题。