Note that there are some explanatory texts on larger screens.

plurals
  1. POJava : Serializable not working to save Object's states
    primarykey
    data
    text
    <p>i am trying simple program in <code>Java to save objects state using Serializable</code> through follwing code , once i writes these objects and then make them to null but <code>when i try to get them back it gives me null values</code> ,should not it give me the stored values ? <code>when i run this program answer is null null null</code> , please see and guide me about it , </p> <p>Game.java </p> <pre><code>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; } } </code></pre> <p>GameCharacter.java </p> <pre><code>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(); } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload