Query for a null-valued nullable attribute

144 views
Skip to first unread message

Jonathan Baxter

unread,
Aug 29, 2018, 9:23:54 PM8/29/18
to cqengine-discuss
I am getting an NPE when attempting to do an equal query for a null value of a nullable attribute. Is this supported? Looking at com.googlecode.cqengine.query.simple.Equal.java it appears not. I can always replace the nulls with a non-null object representing null, but maybe I am doing it wrong?
    protected int calcHashCode() {
      int result = attribute.hashCode();       <----- Ok, attribute knows it is nullable
      result = 31 * result + value.hashCode(); <----- NPE - value being compared is null
      return result;
    }

       



Niall Gallagher

unread,
Aug 30, 2018, 4:24:21 AM8/30/18
to cqengine...@googlegroups.com
Hi Jonathan. You need to use a has() query instead.

Jonathan Baxter

unread,
Aug 30, 2018, 6:05:53 AM8/30/18
to cqengine-discuss
Ah - did not notice that existed. Helps to rtfm. Thanks! Great package btw. Very easy to use.
Message has been deleted

Jonathan Baxter

unread,
Aug 30, 2018, 6:53:39 AM8/30/18
to cqengine-discuss
For anyone who comes across the same issue, a clean way to handle equal for nullable attributes is to rewrite the query:
  /** The equal query for a nullable attribute. */
 
public static <A, O> Query<A> equal(Attribute<A, O> attribute, O value) {
   
return value == null ? not(has(attribute)) : QueryFactory.equal(attribute, value);
 
}


Reply all
Reply to author
Forward
0 new messages