Results 1 to 3 of 3
  1. #1
    ConnorSG1 is offline Member
    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0

    Default 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

  2. #2
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default 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?

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,796
    Rep Power
    19

    Default Re: Saving Object to File and then retrieving it agian

    The first exception says what the problem is.
    You cannot write an object anywhere unless the class is Serializable (ie implements Serializable).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. saving Object[][] via ObjectOutputStream
    By javaSweden in forum Advanced Java
    Replies: 3
    Last Post: 04-23-2012, 11:33 AM
  2. Retrieving object from server RMI
    By nephos in forum Networking
    Replies: 0
    Last Post: 04-20-2011, 09:34 PM
  3. Retrieving the data posted to a JSP file from HTML file
    By marie in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 10-21-2010, 08:37 AM
  4. Retrieving image stored as BLOB object in oracle
    By venkateshcoolmoon in forum JDBC
    Replies: 11
    Last Post: 01-17-2010, 06:06 PM
  5. JDO - Retrieving a persisted object
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-17-2008, 07:46 AM

Posting Permissions

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