Solved Getting rid of "java.io.NotSerializableException" error

Discussion in 'Plugin Development' started by Letroy11, Jan 26, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Letroy11

    I understand that to get rid of this error you need to make the class "Serializable" by having the class implement serializable. My class that is generating the error implements serializable, as well as the class it calls on. I am using ObjectOutputStream as well but am still getting the error.

    Code:
    public void save(Object o, File f) {
            try {
                if (!f.exists()) {
                    f.createNewFile();
                }
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
                oos.writeObject(o);
                oos.flush();
                oos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    Code:
    public class TicketManager implements java.io.Serializable {
    
        /**
         *
         */
        private static final long serialVersionUID = 5438218847836729898L;
        private RazeTickets plugin;
        private int currentTicketID;
    
        private ArrayList<Ticket> allTickets;
    
        public TicketManager(RazeTickets pl) {
            plugin = pl;
            currentTicketID = 1;
            allTickets = new ArrayList<Ticket>();
        }
    Code:
    public class Ticket implements Serializable {
    
        /**
         *
         */
        private static final long serialVersionUID = -3189471872808323979L;
        private int ticketId;
        private TicketStates state;
        private String assignedTo;
        private String closedBy;
        private String reportingPlayer;
        private String reason;
        private String description;
        private Date dateCreated;
        private String location;
        private String world;
        private int commentId;
        private HashMap<Integer, HashMap<String, String>> comments;
     
  2. Offline

    I Al Istannen

    @Letroy11
    If a class is serializable, ALL FIELDS need to be as well. I would guess that RazeTickets isn't. Normally you do not need to serialize the Plugin, as that will change. To tell java to ignore your field, mark it as transient.

    Another way is using Bukkit's ConfigurationSerializable.
     
  3. Offline

    DoggyCode™

    Do you want ConfigurationSerializble?
     
  4. Offline

    Tecno_Wizard

    @DoggyCode™, For what he's doing, The Java Serializer is probably better.
     
Thread Status:
Not open for further replies.

Share This Page