Serialization issue.

Put your problem here if it does not fit any of the other categories.

Serialization issue.

Postby x-files » Mon Jan 14, 2008 9:53 am

Write object works fine.
Read object throws ClassNotFoundException: Snapshot can't found.

write object:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         FileOutputStream fOut = context.openFileOutput(FILE_PATH,
  2.  
  3.                                                 ApplicationContext.MODE_WORLD_READABLE);
  4.  
  5.         ObjectOutputStream osw = new ObjectOutputStream(fOut);
  6.  
  7.         osw.writeObject(snapshot);
  8.  
  9.         osw.flush();
  10.  
  11.         osw.close();
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


and

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. read object:
  2.  
  3.         FileInputStream fis = context.openFileInput(FILE_PATH);
  4.  
  5.         ObjectInputStream ois = new ObjectInputStream(fis);
  6.  
  7.         snapshot = (Snapshot)ois.readObject();
  8.  
  9.         ois.close();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


where
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Snapshot implements Serializable{
  2.  
  3.         private double contentValue;
  4.  
  5.         private double memoryValue;
  6.  
  7. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
x-files
Developer
Developer
 
Posts: 44
Joined: Fri Jan 11, 2008 10:27 am

Top

Postby plusminus » Mon Jan 14, 2008 6:18 pm

Hello x-files,

hm, your code looks really correct :!:

Is all code in the same Activity :?: (probably yes)

does it work without the cast to Snapshot (just putting into object):?: (also probably not the problem of your Exception)

Unfortunately I've got no time to actually test the code, perhaps tonight...

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby x-files » Tue Jan 15, 2008 10:57 am

>> Is all code in the same Activity Question (probably yes)
Yes.

>> does it work without the cast to Snapshot (just putting into object)Question (also probably not the problem of your Exception)

I tried Object obj = ois.readObject();

Exception was throwed here still.


As workarround i implement readObject() and writeObject() methods in Snapshot:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
  2.  
  3.                 contentValue = (BigDecimal)ois.readObject();
  4.  
  5.                 memoryValue = (BigDecimal)ois.readObject();
  6.  
  7.         }
  8.  
  9.        
  10.  
  11.         public void writeObject(ObjectOutputStream oos) throws IOException {
  12.  
  13.                 oos.writeObject(contentValue);
  14.  
  15.                 oos.writeObject(memoryValue);
  16.  
  17.         }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4



It's work fine.

But I don't understand why standart serialization mechanism don't work.
(I mean readObject() when object is user defined class).
x-files
Developer
Developer
 
Posts: 44
Joined: Fri Jan 11, 2008 10:27 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 21 guests