Java : Serializable not working to save Object's states -


i trying simple program in java save objects state using serializable through follwing code , once writes these objects , make them null when try them gives me null values ,should not give me stored values ? when run program answer null null null , please see , guide me ,

game.java

public class game implements serializable{      public game() {     }     public int getsize() {         return size;     }      public void setsize(int size) {         this.size = size;     }      public string gettype() {         return type;     }      public void settype(string type) {         type = type;     }      public string[] getname() {         return name;     }      public void setname(string[] name) {         this.name = name;     }      int  size;      string type ;      string [] name;       public   game (int thesize , string thetype, string[] names )     {          this.size = thesize;          this.type= thetype;         this.name = names;      }  } 

gamecharacter.java

public class gamecharacter extends game {      public gamecharacter(int i, string type, string[] strings) {         // todo auto-generated constructor stub     }      /**      * @param args      * @throws ioexception       * @throws filenotfoundexception       */     public static void main(string[] args) throws filenotfoundexception, ioexception {         game g= new game();          gamecharacter gc1= new gamecharacter(1, "one " , new  string [] {"bow", "sword", "dust"});         gamecharacter gc2 = new gamecharacter(2, "two", new string[] {"one ", "two ", "three"});         gamecharacter gc3= new gamecharacter(3, "three", new string[] {"four ", "five ", "six"});           objectoutputstream oos= new objectoutputstream (new fileoutputstream("game.ser"));         oos.writeobject(gc1);         oos.writeobject(gc2);         oos.writeobject(gc3);         oos.close();                   gc1= null;         gc2= null;          gc3= null;           objectinputstream ois = new objectinputstream(new fileinputstream("game.ser"));          try         {             gamecharacter onerestore= (gamecharacter) ois.readobject();             gamecharacter tworestore  = (gamecharacter) ois.readobject();             gamecharacter threerestore= (gamecharacter) ois.readobject();             system.out.print(onerestore.getname());             system.out.println(tworestore.gettype());             system.out.println(threerestore.getname());             //system.out.println("hello");          }          catch (classnotfoundexception e)          {              e.printstacktrace();         } 

}

your gamecharacter constructor doesn't call constructor game, you're passing in parameters ignored. try running println() statements on original objects; you'll same nulls output. fix , serialization work fine.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -