如何在Grails中调试NPE?

如何在Grails中调试NPE?,grails,groovy,Grails,Groovy,我尝试使用以下代码在Grails中执行原始SQL: class PlainSqlService { def dataSource // the Spring-Bean "dataSource" is auto-injected def newNum = { def sql = new Sql(dataSource) // Create a new instance of groovy.sql.Sql with the DB of the Grails app

我尝试使用以下代码在Grails中执行原始SQL:

class PlainSqlService {

    def dataSource // the Spring-Bean "dataSource" is auto-injected

    def newNum = {
        def sql = new Sql(dataSource) // Create a new instance of groovy.sql.Sql with the DB of the Grails app
        def q = "SELECT a.xaction_id, a.xdin FROM actions a WHERE a.is_approved = 0"
        def result = sql.rows(q) // Perform the query
                return result
    }
}
但我在运行时会遇到此异常。
sql对象不为空
如何调试它

2011-02-13 15:55:27,507 [http-8080-1] ERROR errors.GrailsExceptionResolver  - Exception occurred when processing request: [GET] /moderator/login/index
Stacktrace follows:
java.lang.NullPointerException
    at moderator.PlainSqlService$_closure1.doCall(PlainSqlService.groovy:17)
    at moderator.PlainSqlService$_closure1.doCall(PlainSqlService.groovy)
    at moderator.LoginController$_closure1.doCall(LoginController.groovy:29)
    at moderator.LoginController$_closure1.doCall(LoginController.groovy)
    at java.lang.Thread.run(Thread.java:662)

很难从您提供的有限代码中看出发生了什么,但是有一些事情需要检查。将带有类作用域字段“def plainSqlService”的服务注入到控制器中,是像您在这里为数据源所做的那样,还是您正在调用
new plainSqlService()
?如果您正在创建一个新实例,那么数据源bean不会被注入,groovy.sql.sql构造函数不会失败,但是查询会失败

要尝试的一件事是
grailsclean
——当这样的东西不起作用时,完全重新编译通常会有所帮助

一个重要但不相关的问题是,您不应该在服务中使用闭包。控制器和taglib要求使用闭包实现操作和标记,但服务只是Groovy中定义的Springbean。Spring对闭包一无所知,因为它们只是Groovy执行的一个字段,就像它是一个方法一样,所以您期望从Spring中得到的任何代理(特别是事务行为,还有安全性和其他特性)都不会发生,因为Spring只查找方法

因此
newNum
应声明为:

def newNum() {
   ...
}

如果您想从使用stacktrace进行调试中获得任何帮助,则必须完整粘贴源代码。否则,这些行号毫无意义——除非你期待我们的心理调试。