Serializable


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Serializable

  1. #1
    Join Date
    Jul 2004
    Posts
    40

    Serializable

    Hi there, new here.
    I have a java application which uses serializable classes.
    The serializable classes contain JList objects with DefaultListModels.
    My problem is that I have managed to save the file and then when I open the file again, the ListModel is empty.
    Is this a problem with Java?
    Are ListModels serializable?
    Thanks in advance for any help.
    I think, therefore I am....... I think.

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    i don't know why it's empty, but if it wasn't Serializable it wouldn't let you write it to a file. Everything that is being written has to be serializable or an exception is thrown.

  3. #3
    Join Date
    Feb 2004
    Posts
    808
    dont forget to fill a list with data before writing it to file.. kinda causes empty lists to be written if they dont have anything in them
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    lol that would help

  5. #5
    Join Date
    Jul 2004
    Posts
    40
    Thanks guys but nothing has helped.
    I have a class which is called FileAdminIO which is used to do all the saving and reading etc.
    I have now built little checks which check the ListModels to see if they are empty at the time of saving and reading. When I save the file, I get no error but then when I reload that same file, I get the message that the ListModel is empty.
    This is driving me crazy but I think I will leave it for a few days and come bak with a fresh outlook.
    Thanks for the suggestions though.
    I think, therefore I am....... I think.

  6. #6
    Join Date
    Feb 2004
    Posts
    541
    well without seeing any code I doubt anyone will be able to offer much in depth help. Post the full class on here and someone might see something wrong

  7. #7
    Join Date
    Feb 2004
    Posts
    808
    w3rd
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  8. #8
    Join Date
    Jul 2004
    Posts
    40
    [FONT=courier new][SIZE=1]
    /*
    Firstly the class which handles the File IO
    The ListModels are checked before they are written to file and if they are empty, a warning is printed on screen
    The checks are done again when the file is read and warnings shown if empty.
    */
    Code:
    public class FileAdminIO
    {
      File f = null;
      File dir = null;
    
      public FileAdminIO (String filename)
      {
        // the directory where the file is to be written is initialized
        dir = new File("projects/");
        if (!dir.exists())
        {
          // check if the directory exists, if not: create it
          dir.mkdirs();
          create_fragebogen.showInfoMessage("Directory: "+dir.getAbsolutePath()+" created!");
        }
        f = new File("projects/"+filename);
        try
        {
          if (!f.exists())
          {
    	f.createNewFile();
    	create_fragebogen.showInfoMessage("File: "+f.getAbsolutePath()+" created!");
          }
        }
        catch (IOException ioe)
        {
          create_fragebogen.showErrorMessage("ObjectOutputStream could not be opened: "
    			       + f.getAbsolutePath() + "\nException:" + ioe);
        }
      }
    
      public FileAdminIO (File openFile)
      {
        f = new File(openFile.getAbsolutePath());
        if (!f.exists())
        {
          create_fragebogen.showErrorMessage("File \""+openFile.getAbsolutePath()+"\" doesnīt exist");
        }
      }
    
    
      public void save (Project inProject)
      {
        try
        {
          // Just a warning if there are no Pages to save in Project
          if (inProject.getQPagesListModel().isEmpty())
          {
    	create_fragebogen.showWarningMessage("FileAdminIO save(Project inProject): inProject QPagesListModel is empty!!");
          }
          // creates stream to write to file
          ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream(f.getAbsolutePath()));
          // write the Project Object to file
          oos.writeObject(inProject);
          // write everything from buffer
          oos.flush();
          // close the stream
          oos.close();
        }
        catch (FileNotFoundException fnfe)
        {
          create_fragebogen.showErrorMessage("File could not be opened: " + f.getAbsolutePath());
        //  create_fragebogen.showWarningMessage("Exception: " + fnfe);
        }
        catch (IOException ioe)
        {
          create_fragebogen.showErrorMessage("ObjectOutputStream could not be opened:" + f.getAbsolutePath());
        //  create_fragebogen.showWarningMessage("Exception: " + ioe);
        }
      }
    
      public Project read()
      {
        Project result = null;
        if (f.exists() && (f.length() > 0))
        {
          try
          {
    	// create stream to read data from file
    	ObjectInputStream ois = new ObjectInputStream (new FileInputStream(f.getAbsolutePath()));
    	// read Object out of the stream and cast to Project
    	result = (Project) ois.readObject();
    
    	// Just a warning if there are no pages in loaded Project
    	if (result.getQPagesListModel().isEmpty())
    	{
    	  create_fragebogen.showWarningMessage("FileAdminIO read(): QPagesListModel is empty");
    	}
    
    	// close stream
    	ois.close();
          }
          catch(FileNotFoundException fnfe)
          {
    	create_fragebogen.showErrorMessage("File could not be opened: " + f.getAbsolutePath());
    	System.out.println("Exception: " + fnfe);
          }
          catch (StreamCorruptedException sce)
          {
    	create_fragebogen.showErrorMessage("ObjectOutputStream is corrupt:" + f.getAbsolutePath());
    	System.out.println("Exception: " + sce);
    	//System.exit (0);
          }
          catch (OptionalDataException ode)
          {
    	create_fragebogen.showErrorMessage("Data in ObjectOutputStream is corrupt:" + f.getAbsolutePath());
    	System.out.println("Exception: " + ode);
          }
          catch (ClassNotFoundException cnfe)
          {
    	create_fragebogen.showErrorMessage("Data could not be read from ObjectOutputStream:" + f.getAbsolutePath());
    	System.out.println("Exception: " + cnfe);
          }
          catch (IOException ioe)
          {
    	create_fragebogen.showErrorMessage("ObjectOutputStream could not be opened:" + f.getAbsolutePath());
    	System.out.println("Exception: " + ioe);
          }
        }
        else
        {
          create_fragebogen.showErrorMessage ("No File with the name "+ f.getAbsolutePath() +"found from which a project could be read.");
        }
        // return Project Object to System
        return result;
      }
    
    }
    
    
    /* Now the class Project which is saved to file
    this class has been shortened as the other fields are just Strings with getter and setter methods, they are saved correctly
    */
    public class Project implements Serializable {
      public static int PAGE_COUNTER;           // Counts number of questions in project, Must start with page1
    
      private JList QPages = null;                            // List of all QPages to be saved to disk
      private static DefaultListModel QPagesListModel = null; // Access this to access JList
    }
    Ok, my problem is that when I call the save method, I donīt get a warning if the ListModel is empty, I have tried this with an empty on and I get no warning.
    Now when I read the file in again and check the Project being read, I get the message that the istModel is empty.
    P.S. the class QPage is also serialized and contains JLists, but these are not important yet, all I want is the List of QPage Objects to be read right.
    Appreciate any help but donīt rack your brains about it, I will find a way around it next week.
    Thanks a lot for the input so far.


    ------------

    AAARRRGGGHHHHH!!! my indentation was lost!!!!!
    I think, therefore I am....... I think.

  9. #9
    Join Date
    May 2004
    Posts
    219
    Originally posted by scottfree
    AAARRRGGGHHHHH!!! my indentation was lost!!!!!
    [ code ] tags, bro.

    There needs to be a sticky at the top of each programming forum that says "USE THE **** CODE TAGS WHEN YOU POST CODE," I swear.

  10. #10
    Join Date
    Feb 2004
    Posts
    541
    you can edit what you posted to tidy it up if you want, theres an edit button at the bottom of each post. Simply enclose all the code in [code] tags as drain said (like html, [tag] to start and [/tag] to end).

  11. #11
    Join Date
    Jul 2004
    Posts
    40
    Thanks for the tip Drain.
    Just a little confused as there was no CODE button to add the code tag, but now I know.
    I think, therefore I am....... I think.

  12. #12
    Join Date
    Feb 2004
    Posts
    808
    Originally posted by scottfree
    there was no CODE button to add the code tag, but now I know.
    that's because some dumbarse at jelsoft, decided to put a # symbol on it instead of the word CODE.. look nex t to the PHP button
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  13. #13
    Join Date
    Jul 2004
    Posts
    40
    OK, so that is what it is for.
    I have just been typing my own tags, it is easier and quicker.
    Thanks for the tip.
    I think, therefore I am....... I think.

  14. #14
    Join Date
    Feb 2004
    Posts
    808
    Originally posted by scottfree
    OK, so that is what it is for.
    I have just been typing my own tags, it is easier and quicker.
    Thanks for the tip.
    i do that too, because, probably the same clever individual thought "i know.. let's prompt them for the text they want to CODE-***.. "

    and they used a single line text box prompt..

    hmm, now i dunno about you.. but i dont write lany programs on a single line...

    lol, so yeah.. i write my code tags too.. if youre interested in syntax highlighting. like this:

    Code:
    public class CubbyHole {
      private int contents;
      private boolean available = false;
    
    
      public synchronized int get() {
        //while (available == false) {
          //try {
            // wait for Producer to put value
            //wait();
          //} catch (InterruptedException e) {
          //}
        //}
        available = false;
        // notify Producer that value has been retrieved
        //notifyAll();
        return contents;
      }
      public synchronized void put(int value) {
        //while (available == true) {
          //try {
            // wait for Consumer to get value
            //wait();
          //} catch (InterruptedException e) {
          //}
        //}
        contents = value;
        available = true;
        // notify Consumer that value has been set
        //notifyAll();
      }
    }
    let me know and i'll get a great util for you
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  15. #15
    Join Date
    Jul 2004
    Posts
    40
    Yeah, that looks great.
    Syntax highlighting rocks!
    I use it all the time because I use Textpad to code mostly and then when I read code in a forum, it takes a little longer to find the important parts.
    Thanks.
    I think, therefore I am....... I think.

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