javanotserializableexception

java.io.NotSerializableException by ObjectOutputStream.write Object


I have an Object, that i want write in File:

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

and i have one Runnable, that work with Reflection, to make SwingUtilities.invokeLater() only 1 time.

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

By close the Application i call in Main

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

And then come the Exception

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

This Exception comes only if "File.dat" don't exists by first run. If i don't use UpdateRoutine by first run of Application, "File.dat" can saved without Exception and then i never have this problem.

  1. PersonDaten has not import java.lang.reflect.Method . Why comes this Exception?
  2. How to avoid this Exception?

Solution

  • Is resolved.

    I had PersonDaten as Inner Class.

    Have PersonDaten maked as own class and have no problems.