This discussion is archived
14 Replies Latest reply: Sep 8, 2010 6:01 AM by 843790 RSS

help for saving/loading a class

843790 Newbie
Currently Being Moderated
Hello,
I have a serializable class
public class MyClass implements java.io.Serializable
{...
...
}
I can save it with
fos = new FileOutputStream(jFileChooser1.getSelectedFile().getName());
       out = new ObjectOutputStream(fos);
       out.writeObject(myClassObject);
       out.close();
but when I load it with
fis = new FileInputStream(jFileChooser1.getSelectedFile().getAbsoluteFile());
     
     in = new ObjectInputStream(fis);
      myClassObject= (MyClass)in.readObject();
     in.close();
I get these errors.
Please, I ask you why I get these errors and what can cause them?
Thanks in advance
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: javax.swing.GroupLayout
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1333)
        at java.io.ObjectInputStream.access$300(ObjectInputStream.java:188)
        at java.io.ObjectInputStream$GetFieldImpl.readFields(ObjectInputStream.java:2109)
        at java.io.ObjectInputStream.readFields(ObjectInputStream.java:519)
        at java.awt.Container.readObject(Container.java:3579)
        at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1849)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
        at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1667)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1323)
        at java.io.ObjectInputStream.access$300(ObjectInputStream.java:188)
        at java.io.ObjectInputStream$GetFieldImpl.readFields(ObjectInputStream.java:2109)
        at java.io.ObjectInputStream.readFields(ObjectInputStream.java:519)
        at java.awt.Container.readObject(Container.java:3579)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1849)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
        at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1667)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1323)
        at java.io.ObjectInputStream.access$300(ObjectInputStream.java:188)
        at java.io.ObjectInputStream$GetFieldImpl.readFields(ObjectInputStream.java:2109)
        at java.io.ObjectInputStream.readFields(ObjectInputStream.java:519)
        at java.awt.Container.readObject(Container.java:3579)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
.....
.....

etcetera
Edited by: P5music on Sep 6, 2010 10:54 AM

Edited by: P5music on Sep 6, 2010 10:55 AM
  • 1. Re: help for saving/loading a class
    843790 Newbie
    Currently Being Moderated
    Does MyClass have attributes?. Is every attribute serializable?

    --
    Eduardo Yáñez
    [http://serfj.sourceforge.net] - Simple Ever REST Framework for Java
  • 2. Re: help for saving/loading a class
    843790 Newbie
    Currently Being Moderated
    Thanks a lot for answering.
    These are the included types:
    public boolean 
    public int 
    public int 
    float 
    public int[] 
    public Sequence 
    public String[] 
    public Track[][] 
    public MidiEvent[] 
    public String 
    public ShortMessage
  • 3. Re: help for saving/loading a class
    843790 Newbie
    Currently Being Moderated
    As you can see in the Javadoc: [http://download-llnw.oracle.com/javase/6/docs/api/javax/sound/midi/ShortMessage.html], neither ShortMessage or MidiMessage implement Serializable. I don't know if the rest of attributes implement it, but as I said you in the previous post, every of them must implement Serializable.

    --
    Eduardo Yáñez
    [http://serfj.sourceforge.net] - Simple Ever REST Framework for Java
  • 4. Re: help for saving/loading a class
    843790 Newbie
    Currently Being Moderated
    Thanks again.
    I succeded with
    public class SerializableShortMessage extends ShortMessage implements java.io.Serializable
    {
    ShortMessage content;
    }
    but I am not able to do the same thing with Sequence (and possibly with Track)
    with
    public class SerializableSequence extends Sequence implements java.io.Serializable
    {
    Sequence content;
    }
    because I get
     cannot find symbol
    symbol  : constructor Sequence()
    can you suggest me how to do?
  • 5. Re: help for saving/loading a class
    843790 Newbie
    Currently Being Moderated
    P5music wrote:
    Thanks again.
    I succeded with
    public class SerializableShortMessage extends ShortMessage implements java.io.Serializable
    {
    ShortMessage content;
    }
    But that won't work, it has no sense because content attribute of SerializableShortMessage is not Serializable!
    Also, it won't work for Sequence.

    You need to make a new class that has Serializable attributes that represent the information you need from ShortMessage, Sequence, etc.
    For example, if you need the message that is within ShortMessage, you can make a class:
    public class ShortMessageMessage implements Serializable {
        private byte[] message;
    
        public ShortMessageMessage(byte[] message) {
            ....
        }
    }
    etc.

    --
    Eduardo Yáñez
    [http://serfj.sourceforge.net] - Simple Ever REST Framework for Java

    Edited by: eyp on Sep 6, 2010 1:49 PM
  • 6. Re: help for saving/loading a class
    843790 Newbie
    Currently Being Moderated
    Perhaps I'd better simply save a data chunk without serializing anything because I do not see any convenience in that.
    Am I wrong?
  • 7. Re: help for saving/loading a class
    843790 Newbie
    Currently Being Moderated
    P5music wrote:
    Perhaps I'd better simply save a data chunk without serializing anything because I do not see any convenience in that.
    Am I wrong?
    That's what I told you. Think of what you need to save, do a bean for it so every attribute is serializable, then serialize it. But think about little attributes, not complex classes like ShortMessage or MidiXXX. Save what you'll need to work in the future, or to make those complex classes from that information you saved.

    --
    Eduardo Yáñez
    [http://serfj.sourceforge.net] - Simple Ever REST Framework for Java

    Edited by: eyp on Sep 7, 2010 11:59 AM
  • 8. Re: help for saving/loading a class
    843790 Newbie
    Currently Being Moderated
    Thanks a lot for your patience and useful information.
    What I am saying is that I do not see the usefulness of serialization instead of casting.
    Indeed, if I need to save an entire midi Sequence in the form of data cannot use serialization, nor xml can help me, so I think I have to devise some strategy like splitting my internal stuff in data blocks,taking into account the length of each block in bytes:
    1st block: parameters+array of block lengths
    2st block: Sequence1
    3st block Sequence2
    etc

    Then when I load I read the 1st block with fixed length, then I get the length of other blocks, read them in bytes and then perform a cast (is it possible or also casting is transient "blind"?).
    Am I wrong?
    Sincerely I tought java would perform all this with its serialization, taking into account the nature of classes to save, like a sort of advanced xml, able to handle objects with variable tansient things.
  • 9. Re: help for saving/loading a class
    EJP Guru
    Currently Being Moderated
    Serialization and casting aren't alternatives. The statement makes no sense. Your problem is that you are trying to serialize objects that aren't serialiazable. You can't.
  • 10. Re: help for saving/loading a class
    843790 Newbie
    Currently Being Moderated
    P5music wrote:
    Thanks a lot for your patience and useful information.
    What I am saying is that I do not see the usefulness of serialization instead of casting.
    They are not related, they are completely different things. Casting is related to inheritance, Serialization is related to transformation of objects so they can 'travel' into streams.
    Indeed, if I need to save an entire midi Sequence in the form of data cannot use serialization, nor xml can help me, so I think I have to devise some strategy like splitting my internal stuff in data blocks,taking into account the length of each block in bytes:
    1st block: parameters+array of block lengths
    2st block: Sequence1
    3st block Sequence2
    Read what I said to you in previous posts. If you need some information of objects that can't be serialized, make a class with attributes that store that information.
    For example if you need the message of ShortMessage, that is a byte[], then in your new class add a byte[] attribute. A byte[] attribute is able to be serialized, but
    not ShortMessage class because it doesn't implement java.io.Serializable.

    If you make a class with serializable attributes, and you declare your class as implementing Serializable, then your class and every attribute whithin it will be
    serializable.

    You can serialize an object only if it implements Serializable and every of its attributes implement Serializable.

    --
    Eduardo Yáñez
    [http://serfj.sourceforge.net] - Simple Ever REST Framework for Java
  • 11. Re: help for saving/loading a class
    843790 Newbie
    Currently Being Moderated
    Please take a look at this function (it wasn't made by me):
    public Object toObject (byte[] bytes)
    {
      Object obj = null;
      try {
        ByteArrayInputStream bis = new ByteArrayInputStream (bytes);
        ObjectInputStream ois = new ObjectInputStream (bis);
        obj = ois.readObject();
      }
      catch (IOException ex) {
        //TODO: Handle the exception
      }
      catch (ClassNotFoundException ex) {
        //TODO: Handle the exception
      }
      return obj;
    }
    I think it performs a casting from a byte array to an object. Yes, every object inherits from Object so this deals with inheritance.
    I just need to know if I can cast an entire Sequence from a byte array because I fear that java could not be able to handle the internal amount of midi messages (like with serialization) but I hope not.
    Thanks
  • 12. Re: help for saving/loading a class
    843790 Newbie
    Currently Being Moderated
    P5music wrote:
    Please take a look at this function (it wasn't made by me):
    public Object toObject (byte[] bytes)
    {
    Object obj = null;
    try {
    ByteArrayInputStream bis = new ByteArrayInputStream (bytes);
    ObjectInputStream ois = new ObjectInputStream (bis);
    obj = ois.readObject();
    }
    catch (IOException ex) {
    //TODO: Handle the exception
    }
    catch (ClassNotFoundException ex) {
    //TODO: Handle the exception
    }
    return obj;
    }
    I think it performs a casting from a byte array to an object.
    You have a mess with some concepts. This method deserializes a byte[] into an Object, it doesn't do any casting. The byte[] represents an object
    that is serializable so it's possible to deserialize it.
    Yes, every object inherits from Object so this deals with inheritance.
    I just need to know if I can cast an entire Sequence from a byte array
    NO because Sequence doesn't implement java.io.Serializable.

    What do you need to store?

    --
    Eduardo Yáñez
    [http://serfj.sourceforge.net] - Simple Ever REST Framework for Java
  • 13. Re: help for saving/loading a class
    843790 Newbie
    Currently Being Moderated
    What do you need to store?
    Sequences that are in a class.
  • 14. Re: help for saving/loading a class
    843790 Newbie
    Currently Being Moderated
    P5music wrote:
    What do you need to store?
    Sequences that are in a class.
    You won't be able to do it. You'll need to extract some info from those Sequences that be serializable, or you'll need to store that information in another way and save it in a file with another structure (class), but you never, never will be able to save a Sequence as is, because that class is not serializable.

    --
    Eduardo Yáñez
    [http://serfj.sourceforge.net] - Simple Ever REST Framework for Java