Results 1 to 2 of 2
  1. #1
    teardrop3903 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default Serializing and deserailizing an object

    Can someone tell me whats wrong with my code?

    i get these errors when i run.
    Java Code:
    Errorjava.io.NotSerializableException: OptionSet$Option
    Errorjava.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: OptionSet$Option
    Exception in thread "main" java.lang.NullPointerException
    	at Driver.main(Driver.java:15)
    I guess something went wrong while trying to serialize my OptionSet class but i have no idea how to fix it.


    Here is my full project.

    Driver:
    Java Code:
    
    public class Driver {
    	
    	public static void main(String[] args) {
    		//Build Automobile Object from a file.
    		Automobile FordZWT = FileIO.readFile("FordZWT.txt");
    		//Print attributes before serialization
    		FordZWT.print();
    		//Serialize the object
    		FileIO.serializeAuto(FordZWT);
    		//Deserialize the object and read it into memory
    		Automobile newFordZTW = FileIO.deserializeAuto("auto.ser");
    		//Print new attributes
    		newFordZTW.print();
    	}
    }
    FileIO:
    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class FileIO {
    	
    	
    	public static Automobile readFile(String fileName) {
    		Automobile auto = null;
    		try {
    			FileReader file = new FileReader(fileName);
    			BufferedReader buff = new BufferedReader(file);
    			StringTokenizer st, jt;
    			
    			boolean eof = false;
    			String line = null;
    			
    			while (!eof)
    			{
    				String name = null;
    				if ((line = buff.readLine()) != null) {
    					name = line;
    				}
    				
    				float a = 0;
    				if ((line = buff.readLine()) != null) {
    					a = Float.parseFloat(line);
    				}
    				
    				int b = 0;
    				if ((line = buff.readLine()) != null) {
    					b = Integer.parseInt(line);
    				}
    				
    				auto = new Automobile(name, a, b);
    				
    				for(int i=0;i<b;i++) {
    					if ((line = buff.readLine()) != null) {
    						st = new StringTokenizer(line, "|");
    						String token = st.nextToken();
    						int c = Integer.parseInt(st.nextToken());
    						
    						auto.setOptionSet(i, c, token);
    						
    						for (int j = 0; j < c; j++) {
    							if ((line = buff.readLine()) != null) {
    								jt =  new StringTokenizer(line, "|");
    								String token2 = jt.nextToken();
    								float d = Float.parseFloat(jt.nextToken());
    								
    								auto.setOption(i, j, token2, d);
    							}
    						}
    					}
    			}
    				eof = true;
    			}
    			buff.close();
    		} catch (IOException e) {
    				System.out.println("Error" + e.toString());
    		}
    		return auto;
    	}
    	
    	public static void serializeAuto(Automobile name) {
    		try {
    			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("auto.ser"));
    			out.writeObject(name);
    	        out.close();
    		} catch (IOException f) {
    			System.out.println("Error" + f.toString());
    		}
    	}
    	
    	public static Automobile deserializeAuto(String fileName) {
    		Automobile fresh = null;
    		try {
    			ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
    			fresh = (Automobile) in.readObject();
    		} catch (IOException g) {
    			System.out.println("Error" + g.toString());
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		return fresh;
    	}
    }
    Automobile:
    Java Code:
    import java.io.Serializable;
    
    public class Automobile implements Serializable{
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private String name;
    	private float base_Price;
    	private int number_Of_OptionSets;
    	private OptionSet[] optionSet;
    	
    	public Automobile(String name, float price, int count) {
    		this.name = name;
    		this.base_Price = price;
    		this.number_Of_OptionSets = count;
    		optionSet = new OptionSet[count];
    	}
    
    	public String getName() {
    		return name;
    	}
    	
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public float getBase_Price() {
    		return base_Price;
    	}
    
    	public void setBase_Price(float base_Price) {
    		this.base_Price = base_Price;
    	}
    	
    	public int getNumber_Of_OptionSets() {
    		return number_Of_OptionSets;
    	}
    
    	public void setNumber_Of_OptionSets(int number_Of_OptionSets) {
    		this.number_Of_OptionSets = number_Of_OptionSets;
    	}
    	
    	public OptionSet[] getOptionSets() {
    		return optionSet;
    	}
    
    	public void setOptionSets(OptionSet[] optionSet) {
    		this.optionSet = optionSet;
    	}
    	
    	public OptionSet getOptionSet(String name) {
    		int  i;
    		i = findOptionSet(name);
    		if(i == -1)
    		{
    			System.out.println("Option not Found");
    		}
    		return optionSet[i];
    	}
    	
    	public void setOptionSet(int i, int j, String name) {
    		optionSet[i] = new OptionSet(name, j);
    	}
    
    	private int findOptionSet(String Name) {
    		int i = 0;
    		while(i < number_Of_OptionSets && optionSet[i].getName() != name)
    		{
    			i++;
    		}
    		if (i == number_Of_OptionSets)
    			return -1;
    		else
    			return i;
    	}
    	
    	public void setOption(int k, int j, String name, float price) {
    			optionSet[k].setOption(j, name, price);
    	}
    	
    	public void print() {
    		System.out.println(name);
    		System.out.println(base_Price);
    		System.out.println(number_Of_OptionSets);
    		for (int i = 0; i < number_Of_OptionSets; i++) {
    			optionSet[i].print();
    		}
    	}
    }
    OptionSet with Option inner class:
    Java Code:
    import java.io.Serializable;
    
    public class OptionSet implements Serializable{
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 2L;
    	private String name;
    	private int number_Of_Options;
    	private Option[] options;
    
    	public OptionSet (String name, int count) {
    		this.name = name;
    		this.number_Of_Options = count;
    		options = new Option[number_Of_Options];
    	}
    	
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public Option[] getOptions() {
    		return options;
    	}
    
    	public void setOptions(Option[] options) {
    		this.options = options;
    	}
    	
    	public Option getOption(String name) {
    		int  i;
    		i = findOption(name);
    		if(i == -1)
    		{
    			System.out.println("Option not Found");
    		}
    		return options[i];
    	}
    	
    	public void setOption(int i, String name, float price)
    	{
    		options[i] = new Option(name, price);
    	}
    	
    	private int findOption(String name) {
    		int i = 0;
    		while(i < number_Of_Options && options[i].getName() != name)
    		{
    			i++;
    		}
    		if (i == number_Of_Options)
    			return -1;
    		else
    			return i;
    	}
    	
    	public void print() {
    		System.out.println(name);
    		System.out.println(number_Of_Options);
    		for (int i =0; i < number_Of_Options; i++) {
    			options[i].print();
    		}
    	}
    	
     	class Option {
    		private String name;
    		private float price;
    		
    		public Option (String name, float price) {
    			this.name = name;
    			this.price = price;
    		}
    		
    		public String getName() {
    			return name;
    		}
    		public void setName(String name) {
    			this.name = name;
    		}
    		public float getPrice() {
    			return price;
    		}
    		public void setPrice(float price) {
    			this.price = price;
    		}
    		
    		public void print() {
    			System.out.println(name + " - $" + price);
    		}
    	}
    }
    Text file that i'm reading from:
    Java Code:
    Focus Wagon ZTW
    18445
    5
    Color|10
    Fort Knox Gold Clearcoat Metallic|0
    Liquid Grey Clearcoat Metallic|0
    Infra-Red Clearcoat|0
    Grabber Green Clearcoat Metallic|0
    Sangria Red Clearcoat Metallic|0
    French Blue Clearcoat Metallic|0
    Twilight Blue Clearcoat Metallic|0
    CD Silver Clearcoat Metallic|0
    Pitch Black Clearcoat|0
    Cloud 9 White Clearcoat|0
    Transmission|2
    Automatic|0
    Manual|815
    Brakes Traction Control|3
    Standard|0
    ABS|400
    ABS with Advance Trac|1625
    Side Impact Air Bags|2
    Present|350
    Not Present|0
    Power Moonroof|2
    Present|595
    Not Present|0
    Last edited by teardrop3903; 04-27-2011 at 03:49 AM.

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    As your error message says,
    Java Code:
    java.io.NotSerializableException: OptionSet$Option
    which means that your inner class Option is not Serializable. Make it to implement Serializable and see what happens.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Serializing
    By shashanksworld in forum New To Java
    Replies: 7
    Last Post: 03-29-2010, 05:47 PM
  2. serializing an object to the same source package
    By shyameni in forum New To Java
    Replies: 10
    Last Post: 11-11-2009, 07:43 PM
  3. Replies: 4
    Last Post: 01-10-2009, 01:17 AM
  4. Object not serializing
    By MamboBanda in forum New To Java
    Replies: 1
    Last Post: 08-12-2008, 01:15 AM
  5. How do I omit an object when serializing?
    By Hasan in forum Advanced Java
    Replies: 1
    Last Post: 05-31-2007, 05:18 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
  •