Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Serializing and deserializing enum with time field

  1. #1
    Junior Member
    Join Date
    Dec 2023
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Serializing and deserializing enum with time field

    Hello !

    I don’t understand why I'am getting 'null' when I'm doing deserealization here:

    Here is my enum:
    ---
    package enums.serialize;

    import java.time.LocalDateTime;

    public enum Data
    {
    INSTANCE;

    private LocalDateTime now;


    public void setTime()
    {
    now = LocalDateTime.now();
    }

    public void printSavedTime()
    {
    System.out.println(now);
    }
    }
    ---

    And here is main program:
    ---
    package enums.serialize;

    import java.io.*;

    public class Main
    {
    public static void main(String[] args) throws IOException, ClassNotFoundException
    {
    //Data.INSTANCE.setTime();
    //saveData();
    getSavedData();
    }

    public static void saveData() throws IOException
    {
    try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("./now.ser")))
    {
    oos.writeObject(Data.INSTANCE);
    oos.flush();
    }
    }

    public static void getSavedData() throws IOException, ClassNotFoundException
    {
    System.out.println(new File("./now.ser").exists());

    try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("./now.ser")))
    {
    ((Data) ois.readObject()).printSavedTime();
    }
    }
    }
    ---

    The output is:
    true
    null

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,112
    Thanks
    65
    Thanked 2,718 Times in 2,668 Posts

    Default Re: Serializing and deserializing enum with time field

    Please edit your post and add code tags to preserve the code's formatting. The posted code does not have any indentations.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Deserializing objects
    By Haring01 in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: January 9th, 2013, 03:13 PM
  2. enum class creation to hold time on android
    By jobsfind39 in forum Object Oriented Programming
    Replies: 3
    Last Post: June 21st, 2012, 10:04 AM
  3. Serializing an Abstract Class
    By chronoz13 in forum Object Oriented Programming
    Replies: 0
    Last Post: March 18th, 2012, 03:12 AM
  4. Need help deserializing an integer array.
    By jeremy_ in forum Collections and Generics
    Replies: 10
    Last Post: May 30th, 2011, 11:50 AM
  5. Serializing Listeners
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 11th, 2011, 05:47 PM