#Primitive-type private variables in class are not serializing

1 messages · Page 1 of 1 (latest)

void wyvern
#

I'm making a mock shopping program, and I need to serialize the store's costs, revenues, and profits from a class (StoreInfo.java) to a file so it can be de-serialized and loaded when the program is restarted and save the values. However, for some reason, the values of my private double variables for costs, revenues, and profits are not being saved.

In my program, there was another class (Inventory.java) I also had to serialize, but its data contained variables of a type that is a class, not a primitive. The serialization worked completely fine in that class, and the method used to serialize is the exact same between Inventory and StoreInfo other than the class names.

Just noting that both of these classes, Inventory and StoreInfo, are singletons.

Here are the variables I want to serialize:

    private double totalCosts;
    private double totalRevenue;
    private double totalProfits;

Here is my code for my methods that serialize and de-serialize the StoreInfo data:

    {
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("storeinfo.dat"));
        out.writeObject(instance); //instance refers to the one instance of the singleton
        out.close();
    }

  public void load() throws IOException, ClassNotFoundException
    {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("storeinfo.dat"));
        instance = (StoreInfo) in.readObject(); //instance refers to the one instance of the singleton
        in.close();
    }
fathom kernelBOT
#

Hey, @void wyvern!
Please remember to /close this post once your question has been answered!

void wyvern
#

This is the test code I wrote to test the serialization (omitting the system.outs):

        //add items to the inventory (also buys them for the store)
        Inventory.getInstance().addProduct(1, "Apple", 10, 2.35, 3.00);
        Inventory.getInstance().addProduct(2, "Orange", 15, 1.35, 2.00);
        Inventory.getInstance().addProduct(3, "Bunny", 1, 1.35, 200.00);
        Inventory.getInstance().addProduct(5, "Can", 1, 1.35, 0.5);

        //sell 8 oranges. this should make the cost different.
        StoreInfo.getInstance().sellProduct(Inventory.getInstance().getProductList().get(2), 7);

        //show current store info
        StoreInfo.getInstance().calculateProfits();

        //save current store info
        try
        {
            StoreInfo.getInstance().save();
        } catch (IOException e)
        {
            throw new RuntimeException(e);
        }

        //sell 1 apple. this should make the revenue and profit different
        StoreInfo.getInstance().sellProduct(Inventory.getInstance().getProductList().get(1), 1);
        StoreInfo.getInstance().calculateProfits();

        //load the previous store info to see if it saved
        try
        {
            StoreInfo.getInstance().load();
        } catch (IOException | ClassNotFoundException e)
            throw new RuntimeException(e);
        }
        //revenue and profit should now be reverted to what they were before the apple was sold.

These are my results from the code:

Testing saving store info:
Current store info: 
Costs: 46.45 Revenue: 14.0 Profits: -32.45
Store info saved
Current store info before loading save: 
Costs: 46.45 Revenue: 17.0 Profits: -29.450000000000003
Store info loaded
Current store info after loading save: 
Costs: 46.45 Revenue: 17.0 Profits: -29.450000000000003

As you can see, the store info after loading the save isn't reverted back to what it was when saved.

#

If you need me to provide anymore code, I can

pulsar dagger
#

One day I'll go see my doctor and tell them, "if you need my medical antecedents, I can go back home and get them to bring them to you"

void wyvern
#

fair, im just not sure if i provided enough. i tried to get everything that would relate to the classes without overloading with irrelevant code

pulsar dagger
#

Well, one may wonder why do you offer the option of providing the code that obviously is the one that matters, rather than providing it in the first place

void wyvern
#

in case i made a misjudgement in what i thought was enough information to diagnose without cluttering the channel, but if it isnt that big of a deal to do such, i understand what you mean

#

hm, looks like the whole class code is so long discord puts it in a .txt, which i hope still indents the same as using the 3 backticks

#

This is the StoreInfo class I want to serialize ^

pulsar dagger
#

Don't you save() with all operations here?

void wyvern
#

you are exactly right... and in the other class I mentioned, I did not do that, which is why I was confusing myself. I feel kinda dumb not realizing this before but I guess I lost track of that being at this for a couple hours

#

as simple as this was thank you honestly cause I was missing the solution right in front of my eyes

pulsar dagger
#

Okay, I was assuming something more complex. At least it was easy to solve

void wyvern
#

👌 tyvm!