-1

just started using the Serializable-thingy, I want to save a couple of lists and some other objects, but I can't seem to get some things right.

I have some JLists with their own "DefaultListModel" that I use to sort things in the list, I call the class SortModel, when I run the program I do this:

SortModel sortModel = new SortModel();
JList jList = new JList(sortModel);

Then later when the program runs, objects are added and sorted according to the specified needs, now, when I save the lists and load them again, they're empty.

I also save a object of a class that holds the background for the program (user chooses one themself), after saving it I need to add it again to the program (the object, background is stored in it), I need to add it to the program again not only load it "as it where", plus I have some objects that I added on that object with their own listeners. After I somehow succeeded in loading it, the objects are there but I can't use them, so I figure listeners don't get saved?

* explaining edit The class that is the program extends JFrame, nothing funny about that I think. The "background obect" (call it map) extends JComponent, I add this to (let's call it program for now...) the program and it pops up with the image which it holds. Onto this map I then add objects that also extends JComponent (call them dots), the dots are assigned their own listeners before they're added, the listeners might not be "real" listeners, but they act the same way, they're "MouseAdapter" does that makes any difference?. /explaining edit *

* code edit * code for saving:

FileOutputStream fOut = new FileOutputStream("testFile.mpd");
ObjectOutputStream outSt = new ObjectOutputStream(fOut);
outSt.writeObject(savedMap);

"testFile.mpd" is what it sounds like, I'm quite sure the .mpd shouldn't matter, you can make up your own formats, right? :) (main-class is called Mappedoodle, .mpd sounds reasonable, no?) "savedMap" is an object of said Mappedoodle and holds all lists and other information needed to be saved.

code for loading:

FileInputStream fIn = new FileInputStream("testFile.mpd");
ObjectInputStream inSt = new ObjectInputStream(fIn);    
Mappedoodle openedMap = (Mappedoodle)inSt.readObject();

The information in openedMap is used (well... it should be...) to overwrite certain things in the program. * /code edit *

Adding everything back onto this object, even adding everything back into the lists wouldn't be so hard since that's just some more lists and a few loops, but I feel like I just don't really get Serializable ^^ so, someone care to try to explain why not everything gets saved? And if it is, why I can't access it? But if I can, how? :)

I don't know what more code should be relevant, please tell me what more information you would need to help me solve this, pasting the whole program would be really messy since it's 11 classes and quite a few lines.

Thanks <3

XistenZ
  • 309
  • 1
  • 4
  • 14
  • Consider learning about serialization with smaller simpler code first. – Hovercraft Full Of Eels Jul 23 '11 at 20:18
  • I did try saving/loading a .txt with user input, worked very well! Since I can't find anything about "objects serialization doesn't seem fit for no apparent reason will not get serialized", I thought I'd go and ask the public. The public know stuff. Lots of it. – XistenZ Jul 23 '11 at 20:20
  • 1
    You've got a bug either at serialization or at de-serialization, but hard to say without code. But it would be hard for us or you to tease out the bugs in your large program. Start small and if it fails, post your code. – Hovercraft Full Of Eels Jul 23 '11 at 20:26
  • Edited OP, added more info that could be helpful and the code for saving/loading – XistenZ Jul 24 '11 at 19:32

2 Answers2

1

The code you must show us must be sufficient to demonstrate your error, and I unfortunately must state that yours doesn't. For instance if I use your code in a very simple example (something I recommend that you do), you'll see that it works. My test code:

Mappedoodle.java

import java.io.Serializable;

public class Mappedoodle implements Serializable {
   private static final long serialVersionUID = -1760231235147491826L;
   private String text;
   private int value;

   public Mappedoodle(String text, int value) {
      this.text = text;
      this.value = value;
   }

   public String getText() {
      return text;
   }

   public int getValue() {
      return value;
   }

   @Override
   public String toString() {
      return text + ", " + value;
   }

}

MappedoodleSerialize.java

import java.io.*;

public class MappedoodleSerialize {
   public static void main(String[] args) {
      Mappedoodle savedMap = new Mappedoodle("Fubar", 200);
      FileOutputStream fOut;
      try {
         // your code below
         fOut = new FileOutputStream("testFile.mpd");
         ObjectOutputStream outSt = new ObjectOutputStream(fOut);
         outSt.writeObject(savedMap);
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

MappedoodleDeSerialize.java

import java.io.*;

public class MappedoodleDeSerialize {
   public static void main(String[] args) {
      try {
         // your code below
         FileInputStream fIn = new FileInputStream("testFile.mpd");
         ObjectInputStream inSt = new ObjectInputStream(fIn);
         Mappedoodle openedMap = (Mappedoodle) inSt.readObject();
         System.out.println(openedMap);
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      } finally {
      }
   }
}

This bit of code compiles, runs and outputs as expected. Your error must lie in code that you've not shown us.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thank you for taking the time to try this, unfortunately the problem isn't compilation or the outputs of a objects toString() (which I also tried, and that works well), the problem is that I need to add everything back onto the components. But, that "private static final long serialVersionUID = -1760231235147491826L;" was something new for me, research on that. – XistenZ Jul 24 '11 at 20:00
  • 1
    @XistenZ: I never mentioned that the problem is with compilation or toString(), but rather that you've got a bug either in how you're serializing or de-serializing your objects. There is ***no way** that we can help you or guess what your bug(s) are based on the information that you've provided*. **That's** what I'm trying to say and to prove. If you still need our help, I think that you'll need to provide more information. Consider creating and posting an [SSCCE](http://sscce.org) (please check the link). – Hovercraft Full Of Eels Jul 24 '11 at 20:03
0

This problem was solved 5 years ago, the solution is lost due to bad memory though.

XistenZ
  • 309
  • 1
  • 4
  • 14