java.lang.NullPointerException:无法在空对象上调用方法 get()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48521559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 10:05:56  来源:igfitidea点击:

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

javajenkinsgroovyjenkins-pipeline

提问by user54

I have the next Groovy code which I try to run in Jenkins Pipeline:

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

@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})"
}

But everytime I get java.lang.NullPointerException: Cannot invoke method get() on null objecterror.

但每次我都会java.lang.NullPointerException: Cannot invoke method get() on null object出错。

When I run it from my desktop, everything works as expected:

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

There are 6 astronauts in space right now.

In Jenkins:

在詹金斯:

There are null astronauts in space right now.

Debug output:

调试输出:

<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>

What should I do to make it work?

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

回答by Vineeth Sai

object.get() will give an NullPointerException if the object is null, so you need to check if the object is null or not before you call any method on it. so, an alternative could to check if astros != nulland then call .get()within the if-block.

如果对象为 null,object.get() 将给出 NullPointerException,因此您需要在调用任何方法之前检查该对象是否为 null。因此,另一种方法可以检查 ifastros != null然后.get()在 if 块中调用。

回答by Nitin Dhomse

Handle the null issue inside your code as follows (Use null safe operator and groovy truth concept.)

按如下方式处理代码中的空问题(使用空安全运算符和常规真理概念。)

@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() // added null safe operator here (will handle null pointer exception)

println "There are ${astros?.number} astronauts in space right now."
//iterate if astros value exists.
if(astros){
  astros.people.each { p->
    println " - ${p.name} (${p.craft})"
  }
}

// As you are having json, you need to parse that as follows.

// 因为你有 json,所以你需要按如下方式解析它。

  def slurper = new groovy.json.JsonSlurper()
  def result = slurper.parseText(astros)
  println result
  println result?.number

回答by Marcos

I take it that you created a shared library and is trying to use this in a pipeline?

我认为您创建了一个共享库并试图在管道中使用它?

I have the same problem at the moment, I think it might be a limitation of the Groovy interpreter on Jenkins, similar to how the each loop didn't work until some time ago.

目前我遇到了同样的问题,我认为这可能是 Jenkins 上 Groovy 解释器的限制,类似于每个循环直到前一段时间才起作用。

I've resorted to using this version of http-builderto circumvent that for now. I'll update this if I find a proper solution (please also post an answer if you find anything).

我暂时使用这个版本的http-builder来规避它。如果我找到合适的解决方案,我会更新它(如果发现任何问题,请同时发布答案)。