Patrick
Patrick

Reputation: 1332

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 existent? I want to avoid using class Long, and stick with using primitive long.

Upvotes: 3

Views: 1448

Answers (2)

Riley Lark
Riley Lark

Reputation: 20890

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).

Upvotes: 5

DataNucleus
DataNucleus

Reputation: 15577

java type of Long accepts nulls, long doesn't. Existing data won't have that so are null.

Upvotes: 4

Related Questions