0

I have a class called Libro which contains strings only:

public class Libro {

    private String titolo;
    private String autore;
    private String editore;
    private String sottotitolo;
    private String genere;
    private String dpubb;
    private String lpubb;
    private String soggetto;
    private String isbn;
    private String note;
    private String prezzo;
    private String npag;

    //getters, setters, constructors...
}

Then I created an object called a belonging to the Libro class, and I tried to write it to a coso.dat file

try{
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("C:\\Users\\Workbench\\Documents\\NetBeansProjects\\coso.dat"));
    out.writeObject(a);
    out.close();
}catch(Exception e) {System.out.println("Damn");}

Then I opened the file with NotePad and it seems that I tried to serialize an unserializable class:

¬í {sr  java.io.NotSerializableException(Vx ç†5  xr java.io.ObjectStreamExceptiondÃäk9ûß  xr java.io.IOExceptionl€sde%ð«  xr java.lang.ExceptionÐý>;Ä  xr java.lang.ThrowableÕÆ5'9w¸Ë L causet Ljava/lang/Throwable;L 
detailMessaget Ljava/lang/String;[ 
stackTracet [Ljava/lang/StackTraceElement;L suppressedExceptionst Ljava/util/List;xpq ~     t ginobook.classi.Librour [Ljava.lang.StackTraceElement;F*<<ý"9  xp   )sr java.lang.StackTraceElementa  Åš&6Ý… I 
lineNumberL declaringClassq ~ L fileNameq ~ L 
methodNameq ~ xp   t java.io.ObjectOutputStreamt ObjectOutputStream.javat writeObject0sq ~ 
  \q ~ q ~ t writeObjectsq ~ 
  ôt ginobook.forms.FormNuovot FormNuovo.javat btnCreaActionPerformedsq ~ 
   q ~ q ~ t access$1500sq ~ 
  Œt ginobook.forms.FormNuovo$15q ~ t actionPerformedsq ~ 
  æt javax.swing.AbstractButtont AbstractButton.javat fireActionPerformedsq ~ 
    ,t "javax.swing.AbstractButton$Handlerq ~ q ~ sq ~ 
  ’t javax.swing.DefaultButtonModelt DefaultButtonModel.javaq ~  sq ~ 
  q ~ $q ~ %t 
setPressedsq ~ 
   üt *javax.swing.plaf.basic.BasicButtonListenert BasicButtonListener.javat 
mouseReleasedsq ~ 
  …t java.awt.Componentt Component.javat processMouseEventsq ~ 
  üt javax.swing.JComponentt JComponent.javaq ~ /sq ~ 
  šq ~ -q ~ .t processEventsq ~ 
  ¼t java.awt.Containert Container.javaq ~ 4sq ~ 
  q ~ -q ~ .t dispatchEventImplsq ~ 
  öq ~ 6q ~ 7q ~ 9sq ~ 
  gq ~ -q ~ .t 
dispatchEventsq ~ 
  t java.awt.LightweightDispatcherq ~ 7t retargetMouseEventsq ~ 
  ­q ~ >q ~ 7q ~ /sq ~ 
  rq ~ >q ~ 7q ~ <sq ~ 
  èq ~ 6q ~ 7q ~ 9sq ~ 

ºt java.awt.Windowt Window.javaq ~ 9sq ~ 
  gq ~ -q ~ .q ~ <sq ~ 
  öt java.awt.EventQueuet EventQueue.javaq ~ 9sq ~ 
   aq ~ Hq ~ It 
access$500sq ~ 
  Åt java.awt.EventQueue$3q ~ It runsq ~ 
  ¿q ~ Mq ~ Iq ~ Nsq ~ 
ÿÿÿþt java.security.AccessControllert AccessController.javat doPrivilegedsq ~ 
   Lt 5java.security.ProtectionDomain$JavaSecurityAccessImplt ProtectionDomain.javat doIntersectionPrivilegesq ~ 
   Vq ~ Uq ~ Vq ~ Wsq ~ 
  Ût java.awt.EventQueue$4q ~ Iq ~ Nsq ~ 
  Ùq ~ Zq ~ Iq ~ Nsq ~ 
ÿÿÿþq ~ Qq ~ Rq ~ Ssq ~ 
   Lq ~ Uq ~ Vq ~ Wsq ~ 
  Øq ~ Hq ~ Iq ~ <sq ~ 
   Ét java.awt.EventDispatchThreadt EventDispatchThread.javat pumpOneEventForFilterssq ~ 
   tq ~ `q ~ at pumpEventsForFiltersq ~ 
   iq ~ `q ~ at pumpEventsForHierarchysq ~ 
   eq ~ `q ~ at 
pumpEventssq ~ 
   ]q ~ `q ~ aq ~ hsq ~ 
   Rq ~ `q ~ aq ~ Nsr &java.util.Collections$UnmodifiableListü%1µìŽ L listq ~ xr ,java.util.Collections$UnmodifiableCollectionB €Ë^÷ L ct Ljava/util/Collection;xpsr java.util.ArrayListxÒ™Ça I sizexp    w    xq ~ px

What did I do wrong..? I'm sorry if the question may seem stupid, but I just couldn't find any answer to it

Pshemo
  • 122,468
  • 25
  • 185
  • 269
csilvano
  • 23
  • 4
  • 3
    It's not Serializable because you didn't implement Serializable. – Compass Nov 28 '16 at 16:24
  • So actually you can see the file created on the file system and application that serialized the file finished normally? That would indicate that your application worked fine. Is it possible that you are just seeing some of the content you serialized with additional references to other classes? – hgrey Nov 28 '16 at 16:27
  • Thank you so much! I really can't understand why I didn't find this out.. should I delete the question? – csilvano Nov 28 '16 at 16:28
  • `catch(Exception e) {System.out.println("Damn");}` don't just print only your own error message if you are still learning Java. Print entire stack trace `e.printStackTrace();` so you could learn more about nature of problem and so others who are more advanced could explain info you got from it to you. – Pshemo Nov 28 '16 at 16:29
  • @Phsemo thank you! I will from now on – csilvano Nov 28 '16 at 16:30

0 Answers0