gpt4 book ai didi

java.lang.NullPointerException : Cannot invoke method get() on null object

转载 作者:太空宇宙 更新时间:2023-11-04 10:44:54 25 4
gpt4 key购买 nike

我有下一个 Groovy 代码,我尝试在 Jenkins Pipeline 中运行:

@Grab('io.github.http-builder-ng:http-builder-ng-core:1.0.3')

import static groovyx.net.http.HttpBuilder.configure

def astros = configure {
request.uri = 'http://api.open-notify.org/astros.json'
}.get()

println "There are ${astros.number} astronauts in space right now."

astros.people.each { p->
println " - ${p.name} (${p.craft})"
}

但是每次我收到 java.lang.NullPointerException: Cannot invoke method get() on null object 错误。

当我从桌面运行它时,一切都按预期运行:

There are 6 astronauts in space right now.

在 Jenkins 中:

There are null astronauts in space right now.

调试输出:

<groovyx.net.http.UriBuilder$Basic@4bc2413c scheme=http port=-1 host=api.open-notify.org path=/astros.json query=[:] fragment=null userInfo=null parent=groovyx.net.http.UriBuilder$ThreadSafe@69c6847a useRawValues=null>

我应该怎么做才能让它发挥作用?

最佳答案

如果对象为 null,object.get() 会给出 NullPointerException,因此在调用对象的任何方法之前需要检查对象是否为 null。因此,另一种方法可以检查 astros != null 是否存在,然后在 if block 内调用 .get()

关于java.lang.NullPointerException : Cannot invoke method get() on null object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48521559/

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