gpt4 book ai didi

spring - NullpointerException:无法获取空对象的属性

转载 作者:行者123 更新时间:2023-12-02 00:11:17 24 4
gpt4 key购买 nike

使用 IDEA 并尝试启动以下代码:

package com.myCompany.routing.spring

import com.dropbox.core.DbxRequestConfig
import grails.util.Holders
import spock.lang.Specification

class DropboxSpringConfigSpec extends Specification {
def grailsApplication=Holders.grailsApplication

def "It instantiates and configures the dropboxRequestConfig component"() {
given:
def ctx = grailsApplication.mainContext
//do stuff...
}
}

我收到以下错误:

java.lang.NullPointerException: Cannot get property 'mainContext' on null object

at com.myCompany.routing.spring.DropboxSpringConfigSpec.It instantiates and configures the dropboxRequestConfig component(DropboxSpringConfigSpec.groovy:20)

我最近拉动了我的 VCS,所以代码应该可以工作。

当作为 Grails 测试运行测试时,我收到以下错误:

Error |2015-03-04 13:32:00,989 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application:Missing configuration in Config.groovy: connection.uri.

最佳答案

好吧,看来 Config.groovy 中的一些配置被赋予了一些环境变量的值:

elasticSearch {
connection {
uri = env.ES_URL
username = env.ES_USER
password = env.ES_PASSWORD
}
indexPrefix = 'test-'
}

由于我没有创建相应的环境变量,所以GrailsContextLoader找不到相应的值,计算失败。

在 IDE 中初始化所需的环境变量并以 Grails 测试的方式运行测试解决了问题。

关于spring - NullpointerException:无法获取空对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28853471/

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