[Logo]
Forums Register Login
Importing/Exporting ArrayList with Objects
Hello there, I am new here!
I am currently dealing with a problem, and whatever I've tried doesn't seem to solve the issue.
As the title suggests, I am trying to export an ArrayList of objects to a file. As it seems, this is successful.
I am using this code:

ExportFile() is in the actionPerformed of a Button in my GUI.
Now, when I am trying to import the ArrayList, I come up with an issue...
Even though there are no errors at all and it seems like it is successfully imported, I can't seem to use any methods to display and use the data in my application.
The code I am using to import is this:

I don't seem to find a way to get around this issue. The ArrayLists are initialized in another class called CafeManager (but I extend that class in this one called CafeManagerUI) and there are also classes for the constructors and setters-getters.

Thanks for your time!
Welcome to the Ranch

Marios Louvaris wrote:. . . . The ArrayLists are initialized in another class called CafeManager (but I extend that class in this one called CafeManagerUI)

I am not sure that a good idea, I am afraid. It might be better to have the GUI class with the display and the CafeManager objects as fields.

and there are also classes for the constructors and setters-getters.

I don't understand that bit, but I suspect that doesn't matter.

Thanks for your time!

That's a pleasure

So, you can write your List to a file, but can't read it? I presume you have verified the existence and size of the serialised file. I presume you don't suffer any exceptions whilst writing that file. Please confirm that CafeOrders has an accessible no‑arguments constructor and that it implements Serializable. Please describe what output you get when you try to deserialise the List. Do you suffer any exceptions or error messages? Rather than the loop you have in lines 23‑25, simply write System.out.println(itemOrders); Remember that array lists have overridden toString() methods, and that would be easier to use than the loop.

Have you really been taught to write this sort of comment?

. . .. . .

Such comments don't tell anybody what they don't know already.

Why are you creating a new button in lines 9/3? You appear not to use those buttons, so they simply go out of scope. Please show us how you are calling those two methods via an action listener.

I think we can help if you provide the additional information I asked for, particular more details about the CafeOrders class.
I am initializing CafeOrders like this:

And in the class I have the code to import/export :

This is my whole CafeOrders.
Also forgot to mention, that the file is created successfully in the directory. No errors,no exceptions. When I execute import, it says successful but it doesn't print anything.
First off, reduce this to two methods with no gui.
A scratch project should be enough.
One method takes your List<CafeOrder> and streams it to a file, and the second takes the filename and returns a List<CafeOrder>.
Use the structure of the code you have.

This will show whether you have an issue in the save/load code.

Ensure you stick plenty of logging in there (System.out.println) so you can see where it is going and how many items it is saving/loading.

If that works (and it should) then you know you are probably making an incorrect assumption about your current code, and at that point you might want to stick lots of simple logging into your current code to check everything.
This is the sort of thing:
 

Marios Louvaris wrote:I am initializing CafeOrders like this:
. . .

That doesn't look right. You should be initialising that List in the constructor, not a method. Why are you returning this?

Dave is right: cut down the code to something small and simple.

If you print the List rather than iterating it, what happens? Does it print this?

[]

 
 

Campbell Ritchie wrote:

Marios Louvaris wrote:I am initializing CafeOrders like this:
. . .

That doesn't look right. You should be initialising that List in the constructor, not a method. Why are you returning this?

Dave is right: cut down the code to something small and simple.

If you print the List rather than iterating it, what happens? Does it print this?

[]



Yes, it prints []
In My main CafeManagerUI class

This is also the contents of the file I created using export after making one order. There's a lot of non-recognizeable stuff, but you can clearly see that there is the orderid and the itemname there(Toast).
 

Marios Louvaris wrote:. . . non-recognizeable stuff . . .

That is normal for a serialised file. Only Strings are legible; numbers are stored in binary and may contain non‑printing characters. Some class names are converted to Strings and types of primitives are shown as single letters, e.g. I = int. That suggests you have successfully serialised the order Was that a one‑element List?
 

Marios Louvaris wrote:. . .

No. All those fields should be initialised in the superclass' constructor and not in public methods. If you call those methods, you are actually initialising those fields twice.

. . . Yes, it prints []

In that case you are deserialising the List as an empty List. Is that after the file you quoted was created?
I made one order with the program, exported and then closed the program. Then I tried to import it and this prints in the console [].

The specific file above is just one order. I made another with 3 orders:
 

A few minutes ago, I wrote:. . . In that case you are deserialising the List as an empty List. Is that after the file you quoted was created?

Is there any possibility that you are re‑initialising the List after deserilaising it?
 

Marios Louvaris wrote:I made one order with the program, exported and then closed the program. . . .

Create two classes with main method one of which serialises such a List and the other of which deserialises it. Print the List and see whether it is still empty.
I guess something is wrong and while it exports the ArrayList successfully, it does not import it at all, or at least where it should...
Maybe this part needs tweaking?


 

Campbell Ritchie wrote:

Marios Louvaris wrote:I made one order with the program, exported and then closed the program. . . .

Create two classes with main method one of which serialises such a List and the other of which deserialises it. Print the List and see whether it is still empty.


Ok, working on it.
 

Campbell Ritchie wrote:

Marios Louvaris wrote:I made one order with the program, exported and then closed the program. . . .

Create two classes with main method one of which serialises such a List and the other of which deserialises it. Print the List and see whether it is still empty.


I've also sent you a pm.
 

Marios Louvaris wrote:. . . I've also sent you a pm.

Please keep all discussions on the forum.
 

Marios Louvaris wrote:. . . Maybe this part needs tweaking? . . .

Don't guess about parts needing tweaking. As we told you, write simple code which serialises and deserialises a List. Get that working and then go back to your original code looking for the problem.
 

Campbell Ritchie wrote:

Marios Louvaris wrote:. . . Maybe this part needs tweaking? . . .

Don't guess about parts needing tweaking. As we told you, write simple code which serialises and deserialises a List. Get that working and then go back to your original code looking for the problem.


Alright, but I use the program and the interface to add values to the ArrayLists...

By the way it also give me a hint that variable itemOrders is not used here:
 

Campbell Ritchie wrote:

Marios Louvaris wrote:. . . I've also sent you a pm.

Please keep all discussions on the forum.


Well, once the issue was resolved, I was willing to provide a solution in this post as well according to what I did wrong and how it got fixed. It's just that I shouldn't release the source code since the project is for a coursework.
....
I am feeling stupid...
The issue was that instead of

I should have written:


Thanks for your time whatsoever...
Well done sorting it out. I am surprised you got an empty List printed in the deserialisation method, however.
Nothing? Or something? Like this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss


This thread has been viewed 315 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 18, 2018 22:12:18.