【问题标题】:Cannot invoke method pullLogs() on null object when calling service in controller在控制器中调用服务时无法在空对象上调用方法 pullLogs()
【发布时间】:2013-05-10 17:53:05
【问题描述】:

我搜索了堆栈溢出并发现了一些类似的问题实例,但完成的修复似乎对我有用。 (模拟一例:Grails - Can't call service from Controller --> always get "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

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

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

谢谢!

【问题讨论】:

    标签: grails service


    【解决方案1】:

    在 UserController 你有

    def auditService
    

    然后

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

    应该是

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

    至于“FactoryBean not initialized”的错误,可以直接去掉

    AuditService auditService
    

    AuditService 内部 - 如果您需要在自己的代码中引用 AuditService,则可以使用 this,这不是必需的。

    【讨论】:

    • 仍然没有运气 =(。抱歉我的代码混乱。我一直在修改它。我需要停下来。修改。然后再修改一些。
    • @CoffeePeddlerIntern 这可能是一个愚蠢的问题,但 AuditService.groovy 在grails-app/services 下,而 UserController.groovy 在grails-app/controllers 下,对吧?
    • 是的。但我已经解决了我的问题。我重新检查了这个项目,现在它神奇地起作用了。感谢您的宝贵时间!
    • @CoffeePeddlerIntern 是的,当 grails 发生奇怪的事情时,我经常会发现某种程度的清理(grails clean,或删除target,或删除target,或删除~/.grails)然后重建会神奇地修复它:-)
    • @IanRoberts 您的评论帮助我解决了我的问题。我的服务方法在开发中运行良好,但在生产中失败,并出现与 OP 相同的错误消息。一个 build prod clean 然后 build war 解决了我的问题。
    【解决方案2】:

    您的服务名称是LogService 还是AuditService?如果是AuditService,则控制器中的属性名称不应包含s

    class UserController {
      def auditService //Name should be the same of the service, not in plural
      ...
    }
    

    【讨论】:

    • 我改变了我的源代码。我切换/重命名了一些名称。现在,当我这样做时,我收到错误 Error creation bean with name 'auditService': org.springframework.beans.factory.FactoryBeanNotInitializedException: FactoryBean is not fully initialized yet
    • 我开始尝试一切,我忘记了什么是原创的,或者有时最后有效的 =S。
    猜你喜欢
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多