使用和/或在条件中时出现 NullPointerException

发布于 2024-12-01 19:47:45 字数 1056 浏览 0 评论 0原文

一个非常简单的示例,说明如何使用 createCriteria 方法在 Grails 应用程序中获取数据:

    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")
    }

在具有 或 { 的行上,我收到 NullPointerException,但没有消息。我尝试了各种组合,并密切关注各个地方给出的示例,却发现如果我尝试使用 也会遇到同样的问题。我知道还有其他方法来获取数据,但我非常想使用这种方法。我无法弄清楚自己可能出了什么问题,所以...在 / 上的此类代码块中可能导致 NullPointerException 的原因是什么?

如果我知道与此案例相关的内容,我会提供更多信息。


编辑:在代码示例中添加了 if 子句(检查所有者)。

我进行了更多调查,发现问题出在 if (owner != null &&owner.size() > 0) { 中,更具体地说,变量名称 所有者。当我将名称更改为其他名称时,问题就消失了。如果有人能解释为什么会发生这种情况,那就非常有趣了。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹雨成花 2024-12-08 19:47:45

问题是“owner”是 Groovy 中的保留字。 Groovy Closures 对此进行了描述。
您可以尝试在“owner”周围放置单引号,以便您的代码行显示为:

if ('所有者' != null && '所有者'.size() > 0) {

可能有效。最好的选择是使用新的变量名称以避免任何混淆。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文