开发者

NullPointerException when using and/or in criteria

A very simplified example of how I use createCriteria method for getting data in my Grails application:

    def c = SomeClassOfMine.createCriteria()
    def projects = c.list(max: limit, offset: start) {
        eq("userId", userId)
        if (owner != null && owner.size() > 0) {
            ilike("ownerName", owner + "%")
        }
        if (someParameter && someParameter.size() > 0) {
            or {
                ne("validated", 1)
                ne("validated2", 1)
            }
        }
        order("name", "asc")
    }

On the line having or { I get NullPointerException without message. I have tried various combinations and followed closely the examples given in various places only to find that I get the same problem if I try to use and too. I know that there are other ways to get the data, but I would very mu开发者_JS百科ch like to use this approach. I failed to figure out what might be wrong by myself so... What might cause NullPointerException in such code block on or / and?

I would provide more information if I would know what is relevant in this case.


EDIT: Added an if clause (where owner is checked) to code example.

I have investigated more and find out that the problem is in if (owner != null && owner.size() > 0) { and to be more specific, the variable name owner. When I change the name to anything else the problem is gone. It would be very interesting if someone could explain why this is happening.


The issue is "owner" is a reserved word in Groovy. This is described in Groovy Closures. You can try placing single quotes around 'owner' so that your line of code would read:

if ('owner' != null && 'owner'.size() > 0) {

That may work. Your best option is to use a new variable name to avoid any confusion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜