Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Serializing an ArrayList  RSS feed

 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The trouble I am having is that once ObjectOutputStream does its job, an ArrayList is returned of the exact size it was when it shut down and serialized the state, but the elements are all null. I opened up the resulting file, and I could see the stored data for each element. How do I get around this?

Here is the relevant code:






 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as all the elements in the collection are serializable, it should work.


Anyway, you probably would get more answers, if this was posted in the correct forum.

Moving this to the "I/O and Streams" forum....

Henry
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rusty,



This line will always cause a warning because actually you cast to the raw type (ArrayList). So "data" will hold only type Object, not UserData.
And Object does not implement Serializable.

Isn't it possible that you don't serialize the ArrayList itself but the object that has the array list as a member to solve this casting problem? The object having the ArrayList would serialize it automatically with the proper cast and you would have to cast only to the objects type. Wouldn't help though if the object is generic itself...

Yours,
Bu.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I don't cast it, then it won't compile. data is an ArrayList, what else could I cast it to? Or is there only 2 options: compiler error or warning, and suppressing it is the only way to "fix" it?

The code to serialize it is in the class that has the ArrayList. The only type that is stored in the List is UserData, and it is serializable. Yes, it *should* work but doesn't.

I guess I could serialize each element, but there could potentially be a few hundred or thousand elements, and that is alot of files.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rusty said:
If I don't cast it, then it won't compile.


Of course you have to cast,but casting like
data = (ArrayList<UserData> ois.readObject();
is just the same as
data = (ArrayList) ois.readObject();


I don't think there's a way to deserialize a generic type directly without warning. That's because you can cast really only to raw type.

The code to serialize it is in the class that has the ArrayList. The only type that is stored in the List is UserData, and it is serializable. Yes, it *should* work but doesn't.
I guess I could serialize each element, but there could potentially be a few hundred or thousand elements, and that is alot of files.


No, that's a bad idea.
ArrayList is serializable, UserData is serializable, *should* work.
Perhaps check a console output of the array list before serialization?
Anyway, the problem must be in a part of your code that you didn't post.
BTW, the flush() is missing in the OutputStream part.

Yours,
Bu.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!