Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Tolls

Thread: Serilization problem in a program

  1. #1
    heatblazer is online now Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Serilization problem in a program

    Hello, recently I`ve added a small serilization option to one program, so I want to save a class which is like a container. It is like :
    Java Code:
    final  class Storage implements Serializable {				
    					ArrayList<Person> empl = new ArrayList<Person>();
    					public void addEmp(Person p) {
    							empl.add(p);
    					}
    					public ArrayList<Person> getEmps() {
    							return empl;
    						}
    				}
    So in my case I have 2 codespaces which use it:
    1-st to add content to the class:
    Java Code:
    @Override
    		public void actionPerformed(ActionEvent saveenvnt) {
    				try {
    					store.addEmp(new Person(fname1.getText(), sname1.getText(), "some work" ));
    					PrintWriter outf = new PrintWriter("empl.txt");															
    					for (int i=0; i < store.getEmps().size(); i++) {outf.println(((store.getEmps()).get(i)).getName()); }	
    					outf.close();											
    					adding.dispose();
    																
    					} catch (Exception saveex) { };
    												}
    				});
    2-nd - to serialize the object:
    Java Code:
    @Override
    public void actionPerformed(ActionEvent e) {
    	//iniate new thread here
           System.out.println("Saving object...");
    										
    	try {
    		ObjectOutputStream os = new ObjectOutputStream(
    		new FileOutputStream("TMP.ser"));
    		os.writeObject(store);
    		os.close();
    		} catch (Exception ex) { };
    								}
    						});
    3-rd then I try restore and read the object - this is where nothing happens :(
    Java Code:
    public void actionPerformed(ActionEvent e) {
    	//iniate new thread here
    										
    	try {
    	      FileInputStream restor = new FileInputStream("TMP.ser");
    	ObjectInputStream objects = new ObjectInputStream(restor);
    	Object one  = objects.readObject();
    	Storage st = (Storage) one;
            System.out.println(st.toString());
    	ArrayList<Person> p = new ArrayList<Person>();
            p =  st.getEmps();
    	System.out.println(p.toString());
    	for (int i=0; i < p.size(); i++) {
    				System.out.println((p.get(i)).getName());
    			}
    	restor.close();
    	objects.close();
    	//list = new JList(tmp);
    												//list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    				
    	} catch(Exception eee) {}
    										 
    								}
    						});
    Can you tell me what is going wrong here?

  2. #2
    heatblazer is online now Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Serilization problem in a program

    I`ve fixed it. Apparently I`ve forgoten to implement Serializable for one of the members of the main class.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Serilization problem in a program

    This is why you shouldn't eat exceptions.
    heatblazer likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    heatblazer is online now Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Serilization problem in a program

    Point taken. I`ll handle them with care from now on.

  5. #5
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Serilization problem in a program

    Also i wouldn't catch general Exceptions, id catch the Exception i was expecting like IO etc, then if another gets thrown i know where my code is failing.

Similar Threads

  1. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 03:43 AM
  2. Can anyone tell me problem in this program ??
    By sahildave1991 in forum AWT / Swing
    Replies: 3
    Last Post: 07-02-2010, 05:37 PM
  3. Problem in Program
    By Abbinormal in forum New To Java
    Replies: 9
    Last Post: 01-08-2010, 03:38 AM
  4. Problem with the below program
    By srikanthnambu in forum Java 2D
    Replies: 1
    Last Post: 05-20-2009, 05:43 PM
  5. Program problem
    By arindamchkrbrty in forum New To Java
    Replies: 8
    Last Post: 03-10-2009, 04:58 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •