Problems with my object serialization


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Problems with my object serialization

  1. #1
    Join Date
    Apr 2007
    Posts
    1

    Problems with my object serialization

    Hi everyone,

    Im trying to store an arraylist by serializing it.

    The problem is, that it wont work, im getting the not serializable exception:
    Code:
    java.io.NotSerializableException: ImageResults
    	at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    	at java.io.ObjectOutputStream.writeObject(Unknown Source)
    	at java.util.ArrayList.writeObject(Unknown Source)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    Is there any way around this? do i need to serialize every object in the arraylist or something? My serialising class and the class of the objects which are found in the arraylist are below:

    please help - thanks

    The class where i serialize the arraylist
    Code:
    import java.io.*;
    import java.util.*;
     
    public class FeatureDatabase {
     
    	public void writeToDatabase (ArrayList imageResults) {
     
    		for (int i=0; i<imageResults.size(); i++) {
    			
    			ImageResults ir = (ImageResults)imageResults.get(i);			
    			System.out.println(ir.name);
    		}
    		
    		ObjectOutput output = null;
     
    		try {
    			// use buffering
    			OutputStream file = new FileOutputStream("FeatureDatabase.db");
    			OutputStream buffer = new BufferedOutputStream(file);
    			output = new ObjectOutputStream(buffer);
    			output.writeObject(imageResults);
    		}
     
    		catch (IOException ex) {
    			ex.printStackTrace();
    			System.out.println("\n\nCannot perform output: "+ex.getLocalizedMessage());
    		}
     
    		finally {
     
    			try {
     
    				if (output != null) {
    					// flush and close "output" and its underlying streams
    					output.close();
    				}
     
    			}
     
    			catch (IOException ex) {
    				System.out.println("Cannot close output stream.");
    			}
    		}
    	}
     
    	public ArrayList getDatabase () {}

    My arraylist which im trying to serialize is full of these objects:
    Do i need to make this class extend serializable???????
    Code:
    import java.util.Vector;
     
    public class ImageResults {
     
    	public String name;
    	public String location;
    	private Vector ccvResults;
    	private Vector acResults;
    	private Vector dctResults;
    	private Vector gmResults;
    	private Vector histogramResults;
    	private Vector miResults;
     
            /* getter and setter methods */

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Yes ... the error is telling you that you need to make your ImageResults class implement the Serializable interface. This interface is like Cloneable: there are no methods to implement, you just need to explicitly state that the class can be serialized.

Similar Threads

  1. Problems with search object in Outlook
    By amotto in forum VB Classic
    Replies: 0
    Last Post: 02-14-2006, 10:46 AM
  2. Replies: 1
    Last Post: 07-23-2001, 05:07 AM
  3. Problems using a COM+ object from Windows 98 Clients
    By John Garcia in forum VB Classic
    Replies: 1
    Last Post: 07-19-2001, 12:23 PM
  4. A creates-->B,C. B call-> A call-> C?
    By rneal in forum Java
    Replies: 2
    Last Post: 12-12-2000, 11:41 AM
  5. Re: Passing an object name through a variable
    By mrfelis in forum VB Classic
    Replies: 9
    Last Post: 04-25-2000, 11:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center