Results 1 to 4 of 4
  1. #1
    javaPower is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default Serializable problem

    i can't figure out what's wrong, but what i'm expecting from the last line System.out.println is to print the name Catty, but the string is empty. why?

    Java Code:
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.FileOutputStream;
    import java.io.Serializable;
    
    public class SerializeCat implements Serializable {
    
    	public static void main(String[] args) {
    		Cat cat1 = new Cat("Catty");
    		Cat cat2 = new Cat();
    		
    		try {
    			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("cat.ser"));
    			out.writeObject(cat1);
    			out.close();
    		} catch (Exception e) {} 
    		
    		try {
    		ObjectInputStream in = new ObjectInputStream(new FileInputStream("cat.ser"));
    		cat2 = (Cat) in.readObject();
    		in.close();
    		} catch (Exception e) {}
    
    		System.out.println("The name of cat2 is " + cat2.getName());
    	}
    }

    the class Cat.java

    Java Code:
    public class Cat {
    	private String name;
    	
    	public Cat(String name) {
    		this.name = name;
    	}
    	
    	public Cat() {
    		this("");
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Make the class Cat serialisable, not the class that is testing it's serialisation capability.

  3. #3
    javaPower is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Make the class Cat serialisable, not the class that is testing it's serialisation capability.

    ok, now i'm awake :) thank you.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default

    Also don't eat exceptions.
    At least do printStackTrace()...if you'd done that you would have seen the exception that this caused.

Similar Threads

  1. marking a field Serializable
    By user1234 in forum Advanced Java
    Replies: 9
    Last Post: 05-18-2010, 12:27 PM
  2. Java reflection: find serializable classes in package
    By andrew222 in forum Advanced Java
    Replies: 2
    Last Post: 02-27-2010, 08:36 AM
  3. Serializable problem in writeObject method
    By nouvaki in forum New To Java
    Replies: 3
    Last Post: 01-16-2010, 02:56 PM
  4. Casting to an List<Serializable>
    By Whatty in forum Advanced Java
    Replies: 5
    Last Post: 10-15-2009, 08:47 PM
  5. Implementing Serializable interface
    By javaplus in forum Advanced Java
    Replies: 4
    Last Post: 12-18-2007, 01:29 PM

Posting Permissions

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