Hello everyone.
For my lab, I need to read some values from a text file.
So here are the data values:
Each Option in the above data has a price which is directly below it.Focus Wagon ZTW 18445.0 5 Color 10 Fort Knox Gold Clearcloat 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 Auto 0 Standard -815 Brakes 3 Standard 0 ABS 400 ABS with Advance Trac 1625 Side Impact Air Bags 2 Present 0 Not Present 0 Power Moonroof 2 Present 0 Not Present 0
I'm supposed to instantiate using the data from above, serialize it, and deserialize the Automotive object. I'm getting a nullpointerexception which i post at the bottom of this post.
Thanks! All help is appreciated
Here is what I have so far
Automotive Class
package Model; import java.util.ArrayList; public class Automotive { //Variables private String properties; private String name; private float carBasePrice; private int numberOfOptionSets; public OptionSet[] optSet; ArrayList OptionSetList; //Overloaded Constructors public Automotive(String name, float carBasePrice) { super(); this.name = name; this.carBasePrice = carBasePrice; ArrayList<OptionSet> OptionSetList = new ArrayList<OptionSet>(); } public Automotive(String name) { super(); this.name = name; ArrayList<OptionSet> OptionSetList = new ArrayList<OptionSet>(); } public Automotive() { OptionSet[] optSet = new OptionSet[6]; } public Automotive(float carBasePrice) { super(); this.carBasePrice = carBasePrice; ArrayList<OptionSet> OptionSetList = new ArrayList<OptionSet>(); }public Automotive(String name, Float carBasePrice, int optcount) { optSet = new OptionSet[optcount]; this.name = name; this.carBasePrice = carBasePrice; this.numberOfOptionSets = optcount; } //Getter and Setter Methods public String getName() { return name; } public void setName(String name) { this.name = name; } public double getBaseprice() { return carBasePrice; } public void setBaseprice(float carBasePrice) { this.carBasePrice = carBasePrice; } //Other Class methods public void updateBasePrice(float carBasePrice){ this.carBasePrice = carBasePrice; } public void addOptionSet(OptionSet b) { OptionSetList.add(b); }public void addOptionSet(int a) { //problem optSet[a] = new OptionSet(a); }public void addOptionSet(int index, int size, String name) { optSet[index] = new OptionSet(name, size); }public void setOption(int indexOfOptionSet, int indexOfOption, String name, float price) { optSet[indexOfOptionSet].setOption(indexOfOption, name, price); } public int findOptionSet(String name) { return (optSet.toString().indexOf("name")); }public void updateOptionSet(int i, int newPrice, String newName) { optSet[i] = new OptionSet(newName, newPrice); } public void deleteOption(int l) { try { optSet[l] = null; } catch (NullPointerException e) { //ignored } } public void print() { System.out.println(name); System.out.println(carBasePrice); System.out.println(numberOfOptionSets); for (OptionSet optionset : optSet) { optionset.printOptSet(); } } } //Add print method and a tostring method that is nicely formatted.
OptionSet
FILE IO classpackage Model; import java.util.ArrayList; public class OptionSet { private String name; Option[] OptSet; private Option Options; private int numberOfOption; public OptionSet(String name, int count) //Add constructors with name and opt input. default constructor { this.name = name; OptSet = new Option[count]; this.numberOfOption = count; } public OptionSet(String name) { this.name = name; } public OptionSet(int count) { Option[] OptSet = new Option[count]; for (Option opt: OptSet) { opt = new Option(); } }; //Setters and Getters public String getName() { return name; } public void setName(String name) { this.name = name; } public Option getOption(int i) { return OptSet[i]; }public Option getOption(String name) { return findOption(name); } public double getOptionPrice(String name) { return findOption(name).getBaseprice(); } public void setOption (int i, String name, double price) { OptSet[i].setBaseprice(price); OptSet[i].setName(name); //Setters and Getters End }public Option findOption(String name) { return OptSet[OptSet.toString().indexOf("name")]; } public void updateOption(int i,String newName) { OptSet[i].setName(newName); } public void updateOption(int i, int newPrice) { OptSet[i].setBaseprice(newPrice); } public void deleteOption(int l) { try { OptSet[l] = null; } catch (NullPointerException e) { //ignored } } public void printOptSet() { for (Option Opt : OptSet) { System.out.println((Opt.toString())); } } public class Option { //Make the Option class an inner class of OptionSet (for Lab 1) //Variables private String name; private double baseprice; //Constructors public Option() { } public Option(String name, double baseprice) { super(); this.name = name; this.baseprice = baseprice; } public Option(String name) { super(); this.name = name; } public Option(double baseprice) { super(); this.baseprice = baseprice; } //Getters and Setters public String getName() { return name; } public void setName(String name) { this.name = name; } public double getBaseprice() { return baseprice; } public void setBaseprice(double baseprice) { this.baseprice = baseprice; } public void printOption() { StringBuffer output = new StringBuffer("Name:"); output.append(name); output.append(", Price $"); output.append(baseprice); System.out.println(output.toString()); } } }
Driver Class:package Util; import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; import Model.*; public class Fileio { public static Automotive readDataFile() { Automotive a1 = null; try { String input = null; BufferedReader bf = new BufferedReader(new FileReader("data.txt")); Scanner sc = new Scanner(bf); StringTokenizer tok, eni; boolean procceed = false; while (procceed == true) { if (sc.hasNext()) { input = sc.nextLine(); } float cost = 0; if(sc.hasNext()) { cost = sc.nextFloat(); } int numOptionSets = 0; if(sc.hasNext()) { numOptionSets = sc.nextInt(); } a1 = new Automotive(input, cost, numOptionSets); for (int k = 0; k < numOptionSets; k++) { if(sc.hasNext()) { input = sc.nextLine(); tok = new StringTokenizer(input, ":"); String optSetName = tok.nextToken(); int optSetSize = Integer.parseInt(tok.nextToken()); a1.addOptionSet(k, optSetSize, optSetName); } for (int l = 0; l < k; l++) { if (sc.hasNext()) { eni = new StringTokenizer(input, ":"); String token2 = eni.nextToken(); float d = Float.parseFloat(eni.nextToken()); a1.setOption(k, l, token2, d); } } procceed = false; } } sc.close(); } catch (Exception e) { } return a1; } public static void serialize(Automotive car) { try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("auto.ser")); out.writeObject(car); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Automotive deserialize(String file) { Automotive test = null; try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); test = (Automotive) in.readObject(); } catch (IOException excep) { excep.printStackTrace(); } catch (ClassNotFoundException g) { System.out.println("Error" + g.toString()); } return test; } }
package Model; import Util.Fileio; public class Driver { public static void main(String[] args) { //Build Automobile Object from a file. Automotive FordZWT = Fileio.readDataFile(); //Print attributes before serialization FordZWT.print(); //Serialize the object Fileio.serialize(FordZWT); //Deserialize the object and read it into memory Automotive newFordZTW = Fileio.deserialize("auto.ser"); //Print new attributes newFordZTW.print(); } }
I have this exception:
Exception in thread "main" java.lang.NullPointerException at test.readDataFile(test.java:59) at test.main(test.java:92)