0

我搜索了堆栈溢出并发现了一些类似的问题实例,但完成的修复似乎对我有用。(类似的例子:Grails - Can't call service from Controller --> 总是得到“Cannot invoke method on null object error”

我的服务可以这样总结

class AuditService {


  AuditService auditService

  def sql
  def dataSource
  static transactional = true


  def pullLogs(String username, String id) { 
    if(username != null && id != null) {

        sql = new Sql(dataSource)
        println "Data source is: " + dataSource.toString()
        def schema = dataSource.properties.defaultSchema

        sql.query('select USERID, AUDIT_DETAILS from DEV.AUDIT_LOG T WHERE XMLEXISTS(\'\$s/*/user[id=\"' + id + '\" or username=\"'+username+'\"]\' passing T.AUDIT_DETAILS as \"s\") ORDER BY AUDIT_EVENT', []) { ResultSet rs ->
            while (rs.next()) { 
                def auditDetails = new XmlSlurper().parseText(rs.getString('AUDIT_EVENT_DETAILS'))
                println auditDetails
            }
        }
        sql.close()
    }
}

}

我试图称呼它的方式就像这样

UserController {

  def auditService


  show(Long id){

    def UserInstance = User.get(id)

    //Also tried def auditResults = auditServices.pullLogs(UserInstance.username, UserInstance.id)
    def auditResults = auditServices(UserInstance.username, UserInstance.id)
    System.out.println(" "+ auditResults)
    [UserInstance: UserInstance,params:params]
  }
}

我得到的错误是

Class:
java.lang.NullPointerException
Message:
 Cannot invoke method pullLogs() on null object

我很困惑。(给了我查询)

非常感谢任何想法/意见/帮助!

谢谢!

4

2 回答 2

3

在 UserController 你有

def auditService

但是之后

def auditResults = auditServices(UserInstance.username, UserInstance.id)

应该是

def auditResults = auditService.pullLogs(UserInstance.username, UserInstance.id)

至于“FactoryBean 未初始化”错误,您可以简单地删除

AuditService auditService

从内部-如果您需要从其自己的代码中引用 AuditService ,AuditService您可以使用它。this

于 2013-05-10T18:34:31.220 回答
1

您的服务名称是LogServiceor AuditService?如果它是AuditService控制器中的属性名称,s则不应该。

class UserController {
  def auditService //Name should be the same of the service, not in plural
  ...
}
于 2013-05-10T18:10:29.990 回答