简体   繁体   English

java.lang.NullPointerException:无法在 null 上调用方法 get() object

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

I have the next Groovy code which I try to run in Jenkins Pipeline:我有下一个 Groovy 代码,我尝试在 Jenkins 管道中运行:

@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 object error.但每次我得到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:在 Jenkins:

There are null astronauts in space right now.

Debug output:调试 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?我应该怎么做才能让它发挥作用?

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.如果对象为 null,object.get() 将给出 NullPointerException,因此您需要在调用任何方法之前检查该对象是否为 null。 so, an alternative could to check if astros != null and then call .get() within the if-block.因此,另一种方法可以检查astros != null然后在 if 块中调用.get()

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

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-builder to circumvent that for now.我现在已经使用这个版本的http-builder来规避它。 I'll update this if I find a proper solution (please also post an answer if you find anything).如果我找到合适的解决方案,我会更新它(如果你发现任何问题,请同时发布答案)。

if you facing " java.lang.nullpointerexception cannot invoke " then try to initialize every element of object_array with new class() like:如果您遇到“ java.lang.nullpointerexception 无法调用”,请尝试使用 new class() 初始化 object_array 的每个元素,例如:

Result[] s=new Result[3];

s[0]=new Result();
s[1]=new Result();
s[2]=new Result();

Example:例子:

class hybrid1
    {
    public static void main(String arg[]) 
        {
        Result[] s;
        s=new Result[3];
        //s[0]=new Result();
        int i;
        for(i=0;i<3;i++)
            {
            s[i]=new Result();
            s[i].getroll(101);
            s[i].getmarks(88.36F,78.65F);
            s[i].display();
            }
        
        /*Result s1;
        s1=new Result();
        s1.getroll(101);
        s1.getmarks(58,69);
        s1.display();*/
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.NullPointerException:无法在空对象上调用方法queryForList() - java.lang.NullPointerException: Cannot invoke method queryForList() on null object 放心 - post() - java.lang.NullPointerException: 无法在 null 对象上调用方法 trim() - Rest Assured - post() - java.lang.NullPointerException: Cannot invoke method trim() on null object java.lang.NullPointerException:无法在空对象上获取属性&#39;grailsResourceProcessor&#39; - java.lang.NullPointerException: Cannot get property 'grailsResourceProcessor' on null object java.lang.NullPointerException:无法在空对象上获取属性“ text” - java.lang.NullPointerException: Cannot get property 'text' on null object java.lang.NullPointerException:尝试在空对象引用上调用虚方法,而它不为空 - java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference, while its not null java.lang.NullPointerException:尝试在 oncreate 方法中对空对象引用调用虚拟方法 - java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference in oncreate method java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法… - java.lang.NullPointerException: Attempt to invoke virtual method … on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“RecyclerView$ViewHolder.shouldIgnore()” - java.lang.NullPointerException: Attempt to invoke virtual method 'RecyclerView$ViewHolder.shouldIgnore()' on a null object reference 引发者:java.lang.NullPointerException:尝试在null对象引用上调用接口方法 - Caused by: java.lang.NullPointerException: Attempt to invoke interface method on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚方法&#39;ActionBar.setNavigationMode(int)&#39; - java.lang.NullPointerException: Attempt to invoke virtual method 'ActionBar.setNavigationMode(int)' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM