One_guy
One_guy

Reputation: 11

Saving a singleton object

I know this site isn't made for questions like this but I've been searching for the answer to this I haven't found anything and I need a confirmations.

I have a singleton class which is the centre of my program, in some situations I try to save its state, however it seems it doesn't save properly, and I don't see why because It's not the first time I do this, however It is the first time I try to save a singleton, so is it possible to save a singleton object? Here are my loading and saving codes of this object

 public void Loading(String name) {
    ObjectInputStream is = null;
 //ignore this variable
    game_loaded = 1;
    try {
        is = new ObjectInputStream(new FileInputStream(name + ".dat"));
                 //Logica is the singleton class, 
                 //logi is the name of the variable where it is
        logi = (Logica) is.readObject();
    } catch (FileNotFoundException e1) {
        JOptionPane.showOptionDialog(frame, "Game Invalid", "Load",
                JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE,
                null, new String[] { "Ok" }, "Ok");
        return;

    } catch (IOException e1) {

    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        is.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block

    }

    JOptionPane.showOptionDialog(frame, "Game Loaded Sucessfully", "Load",
            JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE,
            null, new String[] { "Ok" }, "Ok");

}

Save:

    public void saving(String nome){

    ObjectOutputStream os = null;

    try {
        os = new ObjectOutputStream(new FileOutputStream(nome+".dat"));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        return;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        return;
    }
    try {
        os.writeObject(Logica.getLogica(null));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        return;
    }
    JOptionPane.showOptionDialog(frame, "Game Saved sucessfully", "Load",
            JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE,
            null, new String[] { "Ok" }, "Ok");
    if (os != null)
        try {
            os.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

}

EDIT

Ok I may have explained corretcly, it doesn't give me any error loading, however it doesn't load the state I saved, it loads an new "Logica" as if I had created a new one

Upvotes: 1

Views: 1124

Answers (2)

user207421
user207421

Reputation: 310956

The situation you have described is not possible. Ergo you haven't described it correctly. Probably there is something wrong with your observations.

I strongly suspect an IOException or FileNotFoundException, despite your comment in another answer. You have posted code that ignores exceptions in at least four separate places. The presumption is overwhelming.

In fact your exception handling needs a lot of work. You aren't closing the file in case of exceptions for example. There are no finally blocks. You have multiple try/catch blocks where you should have one try and several catches.

Further questions along other lines of enquiry. Is the file being created? With non-zero length? Or else maybe the singleton class only has transient fields?

Upvotes: 0

duffymo
duffymo

Reputation: 308793

There's nothing about Singleton per se that says it can't be serialized; you can write incorrect serialization code for any class. It's not clear what's wrong, and I'm not willing to pore over your code to figure it out, but it should be possible to do.

You have an empty catch block for IOException. That's always a bad idea. You've swallowed the exception that might explain everything. Print the stack trace.

Upvotes: 3

Related Questions