• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serialization/Deserialization of an Array of Objects

 
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe I put "rectCount" into the arrays, and rectCount is 0 now.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If rectCount is the length of the array, I think you mean rectCount - 1.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.,....
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic