| Author |
Serialization/Deserialization of an Array of Objects
|
Jay Orsaw
Ranch Hand
Joined: Jun 14, 2011
Posts: 354
|
|
I have a class named face with methods x, y, width, height.
Face Class:
Plan Class
Then I get this error
I've tried it many ways , but I'm not sure why I'm having an issue... I'm able to read in face by itself, but when I turn it into an array, it gives me issues...
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
You write a Face[0]. That's an empty array. When you read back that array, it's length is still 0. It simply doesn't have an element 0 - it has no elements at all.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Jay Orsaw
Ranch Hand
Joined: Jun 14, 2011
Posts: 354
|
|
|
I believe I put "rectCount" into the arrays, and rectCount is 0 now.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16482
|
|
The only thing you need to fix is the code which assumes the array is non-empty. Just removing line 11, the one which throws the exception, would be what I would do. I don't see what it's supposed to do anyway.
You started out with an empty array, then you did the serialization/deserialization business, and you ended up with an empty array. You wouldn't expect to have it any other way. And if you switched to lists, you would start out with an empty list and end up with an empty list.
|
 |
Jay Orsaw
Ranch Hand
Joined: Jun 14, 2011
Posts: 354
|
|
I believe I understand what you are saying. I should initialize face[0].x,y..... etc to something, then serialize it in, and then deserialize it back out? I figured that since I already initialized x,y,w,h to be 100 each in the code, but I guess that wouldn't effect it.
What I'm trying to do is take the methods of a class called Face and serialize those values and then deserialize them back out and be able to use that information. I am going to have many Face objects.
|
 |
Jay Orsaw
Ranch Hand
Joined: Jun 14, 2011
Posts: 354
|
|
|
I figured out my issue... I initalized the Array of faces to be [rectCount] and rectCount was 0... Not sure why face[0] wouldn't work, but [rectCount + 1] works so I'll keep it that way.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
If rectCount is the length of the array, I think you mean rectCount - 1.
|
 |
Jay Orsaw
Ranch Hand
Joined: Jun 14, 2011
Posts: 354
|
|
Rob Spoor wrote:If rectCount is the length of the array, I think you mean rectCount - 1.
Originally I thought that if you initialized an array to 0 it would work, but I wastold that you need to initalize it to 1 to be the first element 0. I thought I used 0 before, I guess not.,....
|
 |
 |
|
|
subject: Serialization/Deserialization of an Array of Objects
|
|
|