How to add primitive property to existing java datastore entity to avoid null pointer exception
I have existing entities of a type in my gae datastore to which i want to add a new primitive property of primitive type "long":
@Persistent private long bests = 0;
When I do this, when i try to load existing entities which obviously don't have this property set yet, i get:
java.lang.NullPointerException: Datastore entity with kind Player and key Player("patrick") has a null property named bests. This property is mapped to model.Player.bests, which cannot accept null values.
What can I do to avoid this problem? Like a way to default to zero when field is not exi开发者_如何学编程stent? I want to avoid using class Long, and stick with using primitive long.
You could use a Long temporarily, update all of your entities to have a value of zero, and then change the field back to a long. Alternatively, read in all of your data to some file, delete all of your entities, and write them back with the new long field (careful of ownership links being broken).
java type of Long accepts nulls, long doesn't. Existing data won't have that so are null.
精彩评论