• Post Reply Bookmark Topic Watch Topic
  • New Topic

Updating an Object?  RSS feed

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I'm writing a program that writes a Player file using an ObjectOutputStream is there a way to easily update old Objects that have been read, into new Objects with the newly added method calls?

To illustrate:

Old 'A' saved object has;

constructor()
getName()
getHeight()

New 'B' object type has;

constructor()
getName()
getHeight()
getAge()

Clearly when I load object 'A' from the Player class, and then call getAge() I get a NullPointerException due to the object 'A' not having the version 2s ('B's) getAge() method. Is there a simple way to "upgrade" my objects as they are read in?

I hope that I'm making sense



 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't add a new method to class dynamically. If "age" attribute is optional, you can initialize to null in the constructor.
 
Mark Oneill
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The methods shown were just to illustrate. So it's not about optional attributes. The Player class is work in progress and things will be added to it over a period of time. Version 1, Version 2, Version 3, etc. With each new version new getter and setter methods will be added.

I was thinking about setting the serialVersionUID for each version to something different so I could identify if the object was an "old" one or not.

I suppose I will have to load the old object, check the serialVersionUID, pull out the getter methods return values, store them in temporary attributes and then reload them into a new object.

I was just hoping there was an easier/simpler way to do it than this.
 
Rancher
Posts: 3385
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To take your example above, you have saved a Player instance, with the values for name and height.
You then add a new attribute, age, and recompile, and rerun, and load the old version up.

Presumably this gives you a Player instance with a value for name and height, but a null age?

What value do you want that to be?
 
Bartender
Posts: 1305
36
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch Mark.

I hope that you find this community welcoming, knowledgeable and sharing.

Have you thought about using Interfaces with your programming?
You could possibly have an interface for base methods and/or versions.
Then you could get a collection of all the object which support that interface, by passing the need to get the serialVersionUID or whatever.
Here is the official Oracle tutorial on interfaces https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html, which may help you get started.

When you say that the object is saved, are you using a database for this? If so, changing/replacing the POJO may not be too big of a deal.
You could add the corresponding field to the database with a usable/valid default value and then replace the POJO.
If you do not want to use a "big" fully functional RDBMS then you could possibly get away with using something like SQLite  (https://www.sqlite.org/)
 
Mark Oneill
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies guys - a great community here. And I am glad to see the site is still up and running - I used to visit here often in Java 1.2 days ;) and have recently started to get back into programming. . .

I am saving each players data to an individual player binary file using



I'm then reading in the file again at a later date using





Within the Player class (version 2) I have a ArrayList with a method addToChatLog(String input) {array_list.add(input;}

It was when I called this method from a version 1 Player that I got the NullPointerException.

I suspect I may be doing something fundamentally wrong :/



 
Dave Tolls
Rancher
Posts: 3385
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All that's wrong is that you haven't initialised the chat log ArrayList.
That way, when you read in an old Player then they will get an empty chat log, but a Player that was saved from the point the chat log existed would get that loaded up properly.
 
Mark Oneill
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I thought I had initialised the ArrayList? :/
 
Mark Oneill
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies for my dumbness I haven't coded in almost 10 years!
 
Dave Tolls
Rancher
Posts: 3385
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, it's me being a bit dim.
The deserialisation skips normal object creation, so that instantiation of the List doesn't happen.
 
Mark Oneill
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Sorry, it's me being a bit dim.
The deserialisation skips normal object creation, so that instantiation of the List doesn't happen.



Ahh I did not know that! Thank you so much for spreading the knowledge

So if I create a method something like:



And then call this method in the "blank" constructor



That should stop my NullPointerExceptions?
 
Dave Tolls
Rancher
Posts: 3385
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor doesn't get called at all.
You can test that (which is what I did) by simply putting a System.out.println("In constructor") in there and it doesn't get called.

What I would suggest is adding a check in the addToChatLog method to see if the list is null, and if so then create a new one.
That would solve your issue without too much jumping through hoops.
 
Mark Oneill
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your help Dave Tolls I will add a bunch of System.out.println() calls and see if I can see whats going on and where it's breaking!

Have a fantastic weekend!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!