-
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.
-
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.
-
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
-
-
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.
-
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
-
-
[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.
-
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.
-
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).
-
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.
-
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
-
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.
-
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
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks