0
votes

I'm trying to implement a Queue with Objectify:

void addQueue(String bucket, String value){
    Long next = ofy().load().type(OfyQueue.class)
            .order("-id").first().get().getId(); 
    OfyQueue q = new OfyQueue(bucket, value, next);
    ofy().save().entity(q).now();
}
void removeQueue(String bucket, String value){
    OfyQueue q = ofy().load().type(OfyQueue.class)
            .filter("value", value).order("-id").first().get();
    ofy().delete().entity(q);
}

There's something wrong with this code:

  • next might be null? So when I create a new OfyQueue it will pass a null value
  • in the removeQuery method, q might be null too, do I need to explicitly test if q is null or ofy().delete().entity will just ignore null values pass to it
1

1 Answers

1
votes

Assuming that QfyQueue.getId() returns the field annotated with @Id, it will never return null in the addQueue() method. However, first().get() will return null in the case that there was no element matching the criteria. You can call safeGet() if you want a thrown exception instead.

In removeQueue() you must explicitly check for a null 'q'. Passing null into the delete method will produce a NPE.