Serializing and deserailizing an object
Can someone tell me whats wrong with my code?
i get these errors when i run.
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:
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:
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:
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:
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:
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