How to add a primitive property to an existing Java storage object to throw an exception from a null pointer

I have existing type objects in my gae datastore, to which I want to add a new primitive property of the primitive type "long":

@Persistent     private long bests = 0;

When I do this, when I try to load existing objects that obviously do not yet have this property, I get:

java.lang.NullPointerException: A Datastore object with a good player and a key player ("patrick") has a null property called bests. This property maps to model.Player.bests, which cannot accept null values.

What can I do to avoid this problem? How is the default method for zero when the field does not exist? I want to avoid using the Long class and stick with using the primitive for a long time.

+3
source share
2 answers

You can use Long for a long time, updating all of your entities to have a null value, and then change the field to long again. Alternatively, read all of your data in a file, delete all of your entities and write them down with a new long field (carefully breaking).

+5
source

java type Long is null, long does not. Existing data will not be null.

+4

Source: https://habr.com/ru/post/1771126/


All Articles