gpt4 book ai didi

java - Grails - 无法从 Controller 调用服务 --> 总是得到 "Cannot invoke method on null object error"

转载 作者:行者123 更新时间:2023-12-01 07:41:18 25 4
gpt4 key购买 nike

我有一个 grails 应用程序,我正在按照此处的教程进行操作:
http://www.grails.org/Services

我有一些代码,例如

import org.springframework.beans.factory.InitializingBean

class SubmitRequestService implements InitializingBean{
def GrailsApplication1
def setting

void afterPropertiesSet(){
this.setting = GrailsApplication1.config.setting
}

def void sendHistoricalContract(HistoricalContract hc_instance){
//... blah blah whatever code
}
}

class SubmitRequestController {
def submitRequestService
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

def index = {
// .... blah blah whatever code
submitRequestService.sendHistoricalContract(historicalContractInstance)
}
}

不,无论我做什么,我似乎都无法将服务注入(inject)到 Controller 中。每当我到达调用服务的行时,我都会收到错误:

ERROR errors.GrailsExceptionResolver - Cannot invoke method sendHistoricalContract() on null object

我做错了什么?

提前致谢

最佳答案

GrailsApplication1 看起来很奇怪 - 这是从哪里来的?如果您想访问 GrailsApplication 实例来获取配置,请对 grailsApplication Spring bean 使用依赖项注入(inject):

class SubmitRequestService implements InitializingBean {

private setting

def grailsApplication

void afterPropertiesSet() {
setting = grailsApplication.config.setting
}

void sendHistoricalContract(HistoricalContract hc_instance) {
//... blah blah whatever code
}
}

我的猜测是 GrailsApplication1 导致服务出现问题,导致其无法注入(inject) Controller ,但我认为它会在启动时因异常而崩溃。尝试运行 grails clean 来强制完全重新编译。

关于java - Grails - 无法从 Controller 调用服务 --> 总是得到 "Cannot invoke method on null object error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4559270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com