2

I'm trying to serialize this Configuration object, but for the life of me I can't get it. I've checked everywhere including StackOverflow. Any help would be greatly appreciated.

Serialization Code:

public void serialize(String outFile)
        throws IOException {
    Configuration config = new Configuration().getConfiguration();
    System.out.println(config.email);
      try
      {
         FileOutputStream outputFile =
         new FileOutputStream("/home/jason/Desktop/config.ser");
         ObjectOutputStream objectOutput = new ObjectOutputStream(outputFile);
         objectOutput.writeObject(config);
         objectOutput.close();
         outputFile.close();
      }catch(IOException i)
      {
          i.printStackTrace();
      }
      System.out.println(config);
}

public void deSerialize()
        throws FileNotFoundException, IOException, ClassNotFoundException {

    Configuration config = new Configuration().getConfiguration();
      try
      {
         FileInputStream inputFile = new FileInputStream("/home/jason/Desktop/config.ser");
         ObjectInputStream objectInput = new ObjectInputStream(inputFile);
         config = (Configuration) objectInput.readObject();
         config.setConfiguration(config);
         objectInput.close();
         inputFile.close();
      }catch(Exception e)
      {
          e.printStackTrace();
      }

      System.out.println(config);

}

And then I call it with the following code:

DataStore data = new DataStore().getInstance();
        try {
            data.deSerialize();
        } catch (ClassNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

DataStore is a simple singleton, and takes no input parameters. It doesn't have any real variables, but simply employs the serialization functions.

Finally, here is my Configuration code:

public class Configuration implements Serializable{
private static final long serialVersionUID = 1388190376766023647L;
public static String email = "";
private static String ip = "";
private static String password = "";
private static String importNumber;
private static Configuration configuration;
private static int singleton = 0;
public String value_of_da_message;

public Configuration()
{}

public Configuration getConfiguration()
{
    if(singleton == 0){
        configuration = new Configuration();
        singleton++;
        return configuration;
    }

    else
        return configuration;
}

public void setConfiguration(Configuration config)
{
    configuration = config;
}

Any clue on where I'm going wrong? Thanks for the help.

user207421
  • 305,947
  • 44
  • 307
  • 483
Peter Barnett
  • 201
  • 1
  • 2
  • 13
  • Probably a daft question, but have to ask as you haven't shown it: You definitely did implement the `Serializable` interface didn't you? – JonK Apr 19 '14 at 20:08
  • Oh good catch. But yes I have implemented it. – Peter Barnett Apr 19 '14 at 20:11
  • 1
    "*Can't Serialize My Object*" what happens when you try? Do you see any exceptions/errors? Also you didn't show us how you declared `Configuration` class. – Pshemo Apr 19 '14 at 20:11
  • Nope. No errors or exceptions. Simply ignores my serialization. I have tested that it is serializing correctly, and it does. So the problem, I believe, is either during deserialization or configuration. And i'll add that! Edit: Added it – Peter Barnett Apr 19 '14 at 20:14
  • Should I not be serializing configuration? – Peter Barnett Apr 19 '14 at 20:16
  • Just a side note: why does your singleton have almost only static variables? IIRC static fields are not (de)serializable. – Thomas Apr 19 '14 at 20:17
  • I suppose you're talking about the Configuration. In that case, it's because I need to use those variables throughout the entire program, and I can't have them changing. EDIT: Just tried un-static-izing everything. Nothing changed. – Peter Barnett Apr 19 '14 at 20:18
  • Don't "un-static-ize" the singleton reference though. – Thomas Apr 19 '14 at 20:31

1 Answers1

2

Have a look here: Java static serialization rules?

Change your fields to be instance fields, which would better fit the singleton approach anyways.

The fields might be serialized but deserialization normally ignores static fields.

Additionally, it looks like you're reading to the temporary object:

config.setConfiguration(config);

This uses the read object twice and doesn't set the data of the singleton unless you keep that a static field (i.e. configuration in the Configuration class is the reference to the singleton and needs to be static)

Update:

Another hint: the static variable singleton might cause problems and is unneeded anyway. Just check configuration == null instead of singleton == 0.

Hint:

I try to help you with serialization in general, but in your special case you might want to use Properties instead which have built-in serialization features (.properties- and xml-files supported) and which are meant for configuration.

Community
  • 1
  • 1
Thomas
  • 87,414
  • 12
  • 119
  • 157
  • Ok, so I'm a bit confused now. I've un-static-ized everything but the singleton, and still nothing. I then tried re-static-izing everything but the Configuration, but still nothing. – Peter Barnett Apr 19 '14 at 20:35
  • Yep. There is nothing I can find wrong. Plus, there are no errors or warnings. – Peter Barnett Apr 19 '14 at 20:46
  • @user3010468 So after deserialization the read object has the correct values but they are different in another place? Or are the wrong values being deserialized? Did you serialize again after your code changes? – Thomas Apr 19 '14 at 20:56
  • Ok so I set my email string public to test all this. Right before serialization, config.email is correct. Right after serialization, config.email is correct. During deserialization, config.email, right after calling config.setConfiguration(), is blank. The same if I call it from config.getConfiguration().email right after calling the setter. – Peter Barnett Apr 19 '14 at 20:58
  • New thing I just noticed. When I de-static-ize the Configuration object in the "Configuration" class, it now becomes null. Whenever I try printing it's value, instead of giving me the normal garbage, it is now always null. Which obviously is no good. – Peter Barnett Apr 19 '14 at 21:00
  • @user3010468 `email` isn't static anymore? Note that `configuration` is the _only_ field that needs to be static. Read up on singletons for the reasons why. – Thomas Apr 19 '14 at 21:01
  • Yea I know, but I'm trying everything now. Configuration static, everything else not, and it isn't accessible. – Peter Barnett Apr 19 '14 at 21:09
  • Alright last try here, perhaps it's a problem with my singleton? Or how I access it? Because when I don't make email or ip or password static, then their values become blank the next time I access configuration. Am I accessing configuration wrong? – Peter Barnett Apr 19 '14 at 21:10
  • GOT IT! I had to keep my "configuration" static only + my singleton variable has to be static. duh! I must have undid that when you told me to un-static-ize everything. I shall now accept your answer. Thanks for your help. :) – Peter Barnett Apr 19 '14 at 21:17
  • 1
    @user3010468 glad you got it working. A final note: as I said before, you don't actually need the `singleton` variable. – Thomas Apr 21 '14 at 17:35