Forums Register Login

Serializing ArrayList?

+Pie Number of slices to send: Send
Hey guys i was just trying to make a login program, i have an ArrayList of User objects (User is a class i made myself). Serializing the arraylist work very well however when i try to "resurrect" my arraylist is empty. When i print the serialized data using println it prints out nicely i think the problem is when i cast the serialized data to ArrayList<User>. It would be great if you could adress my problem however if you see anything else hat i do wrong don't hesitate to criticize as long as i learn something its all good
Here is the serialize class



And this is the main method



Thanks for your help
1
+Pie Number of slices to send: Send
At line 11, you clear the list. (That part I guess is obvious to you.)

At line 12, you call the Reserect method, which deserializes from the file and assigns the result to a local variable inside the Reserect method. That has no effect on the variable in your "main" method.

If the Reserect method is supposed to be returning a list, then it should do that. There's also no point in passing it a list, because it isn't going to do anything with an existing list. It's going to produce a new one. So its signature should be like this:

+Pie Number of slices to send: Send
 

Paul Clapham wrote:At line 11, you clear the list. (That part I guess is obvious to you.)

At line 12, you call the Reserect method, which deserializes from the file and assigns the result to a local variable inside the Reserect method. That has no effect on the variable in your "main" method.

If the Reserect method is supposed to be returning a list, then it should do that. There's also no point in passing it a list, because it isn't going to do anything with an existing list. It's going to produce a new one. So its signature should be like this:



Oh ok thank you, I understand my mistake, I had originally thought that by passing a List to the Reserect method it would alter the List that i pass it. But i also thought that the list would keep the changes, i guess it doesnt store the change in the original location of the List but rather locally in the Reserect method. Is that right?
+Pie Number of slices to send: Send
I think you have it right, but just to be clear, here's what's happening.

In your "main" method you create a List object and assign it to the variable "lur". Then you call some methods which populate that list with a User object. To be precise, what you now have is a List variable named "lur" which contains a reference to a List object which is stored in the heap somewhere. It's important to keep those two things (variable and object) separate, because if you don't then you get confused.

Now, when you call your "Reserect" method, you pass a copy of the "lur" variable to the method. Not a copy of the object, but a copy of the variable. So now inside the Reserect method you have a variable named "lu" (that's the parameter) which contains a reference to that same List object. Okay so far? We have two variables which both refer to the same List object.

Next, at line 25 inside Reserect you call "os.readObject()". This produces a new List object, which came from the file so it contains the data you originally serialized. And then you assign a reference to that new List object to the "lu" variable. So now the "lur" variable in the main method refers to the original List (the one which you cleared) and the "lu" variable in the Reserect method refers to the new List (the one which came from the file).

Finally you return from the Reserect method. Its local variables (including "lu") vanish because their scope was only inside Reserect. But back in the "main" method the variable "lur" is still there, still pointing at the original List. Not at the one which came from the file -- that one now has no variables referring to it any more.
+Pie Number of slices to send: Send
 

Paul Clapham wrote:I think you have it right, but just to be clear, here's what's happening.

In your "main" method you create a List object and assign it to the variable "lur". Then you call some methods which populate that list with a User object. To be precise, what you now have is a List variable named "lur" which contains a reference to a List object which is stored in the heap somewhere. It's important to keep those two things (variable and object) separate, because if you don't then you get confused.

Now, when you call your "Reserect" method, you pass a copy of the "lur" variable to the method. Not a copy of the object, but a copy of the variable. So now inside the Reserect method you have a variable named "lu" (that's the parameter) which contains a reference to that same List object. Okay so far? We have two variables which both refer to the same List object.

Next, at line 25 inside Reserect you call "os.readObject()". This produces a new List object, which came from the file so it contains the data you originally serialized. And then you assign a reference to that new List object to the "lu" variable. So now the "lur" variable in the main method refers to the original List (the one which you cleared) and the "lu" variable in the Reserect method refers to the new List (the one which came from the file).

Finally you return from the Reserect method. Its local variables (including "lu") vanish because their scope was only inside Reserect. But back in the "main" method the variable "lur" is still there, still pointing at the original List. Not at the one which came from the file -- that one now has no variables referring to it any more.



Thank you, I did a small change so the the Reserect method returns a reference to the new list from the file. But thank you even more for the step by step break down, the part about the which variable contains a reference to what was really helpful and cleared up a lot of things.

I guess I have to brush up on my theory, after all I just do these programs for fun rather then taking courses at school so I learn more by experimenting rather then through theory. Thanks so much for your time and detailed break down again. By the way it was you're 13700 post congratulations!
This tiny ad is suggesting that maybe she should go play in traffic.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1204 times.
Similar Threads
Serialization of static variables
Question about clone a Hashtable
Serialization Handing
Serializing more than one value manually
Serialization Problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:24:21.