Results 1 to 3 of 3
- 11-25-2012, 11:08 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 14
- Rep Power
- 0
Saving Object to File and then retrieving it agian
I am trying to save an object named car1 of a class named car(only contains one String valiable Colour) to file and then retrieve it again however this is what i get:
java.io.NotSerializableException: SaveEntireObjectToFile.Car
at java.io.ObjectOutputStream.writeObject0(ObjectOutp utStream.java:1180)
at java.io.ObjectOutputStream.writeObject(ObjectOutpu tStream.java:346)
at SaveEntireObjectToFile.Main.writeObject(Main.java: 41)
at SaveEntireObjectToFile.Main.main(Main.java:121)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main (AppMain.java:120)
Exception in thread "main" java.lang.NullPointerException
at SaveEntireObjectToFile.Main.readObject(Main.java:1 08)
at SaveEntireObjectToFile.Main.main(Main.java:124)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main (AppMain.java:120)
I think the problem lies with retrieving the object. Thanks for any help, my code is below:
Java Code:public class Main { //create an instance of the Car class named car1; static Car car1; static Car car2; public static void writeObject() { //start a fileOutputStream object; FileOutputStream saveFile = null; //start a ObjectOutputStream object; ObjectOutputStream saveObject = null; //store the name of the file to save to in myObjectFile; String myObjectFile = "ObjectFile.txt"; try { //create a fileOutPutStream to link to a file to write to, named objectFile.txt saveFile = new FileOutputStream("object.txt"); //create an objectOutputStream to place objects into save file saveObject = new ObjectOutputStream(saveFile); //write a complete object to objectOutputStream using writeObject() method; saveObject.writeObject(car1); } //end of try; catch (FileNotFoundException ex) { System.out.println("Sorry file not found"); } //end of catch; catch(IOException ex) { ex.printStackTrace(); } //end of catch; //everything in the following will always be executed regardless; finally { try { //if there is something in the file to send; if (saveObject !=null) { //flush the output stream before closing the file; saveObject.flush(); //close the door behind me; saveObject.close(); }//end of if statement; } //end of try statement; catch(IOException ex) { ex.printStackTrace(); } //end of catch statement; } //end of finally statement; } //end of method; public static void readObject() { FileInputStream readFile = null; ObjectInputStream getObject = null; try { //open file to read from, named ObjectFile.txt; readFile = new FileInputStream("ObjectFile.txt"); //create an ObjectInputStream to get objects from readFile; getObject = new ObjectInputStream(readFile); car2 = (Car)getObject.readObject(); //close File (This also closes saveFile); getObject.close(); }//end of try; catch(Exception exc) { System.out.println("Sorry there was an error"); }//end of catch; System.out.println(car2.toString()); }//end of method; public static void main(String[] args) { car1 = new Car("Red"); //write it to a file; writeObject(); //read the complete object back in from the new Car object; readObject(); }//main }//class
- 11-26-2012, 12:33 AM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Re: Saving Object to File and then retrieving it agian
How are you "retrieving" it without a return statement?
Also, would you paste the class Car?
- 11-26-2012, 11:12 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
saving Object[][] via ObjectOutputStream
By javaSweden in forum Advanced JavaReplies: 3Last Post: 04-23-2012, 11:33 AM -
Retrieving object from server RMI
By nephos in forum NetworkingReplies: 0Last Post: 04-20-2011, 09:34 PM -
Retrieving the data posted to a JSP file from HTML file
By marie in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 10-21-2010, 08:37 AM -
Retrieving image stored as BLOB object in oracle
By venkateshcoolmoon in forum JDBCReplies: 11Last Post: 01-17-2010, 06:06 PM -
JDO - Retrieving a persisted object
By Java Tip in forum Java TipReplies: 0Last Post: 03-17-2008, 07:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks