我搜索了堆栈溢出并发现了一些类似的问题实例,但完成的修复似乎对我有用。(类似的例子: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
我很困惑。(给了我查询)
非常感谢任何想法/意见/帮助!
谢谢!