If statement If语句未使用

If statement If语句未使用,if-statement,groovy,nullpointerexception,If Statement,Groovy,Nullpointerexception,我在Groovy中有以下代码: seedGerritHost = config.seedgerrit?.host println "seedGerritHost: " + seedGerritHost if (seedGerritHost == null) { seedGerritHost = config.gerrit.host.toString() print "seedGerritHost was null, now its: " + seedGerritHost } ..

我在Groovy中有以下代码:

seedGerritHost = config.seedgerrit?.host
println "seedGerritHost: " + seedGerritHost
if (seedGerritHost == null) {
    seedGerritHost = config.gerrit.host.toString()
    print "seedGerritHost was null, now its: " + seedGerritHost
}
...
def predefineJobs = new File("/usr/share/jenkins/predefineJobs")
def newPredefineJobs = predefineJobs.text
    .replace("#projectName",config.jenkins.project.toString())
    .replace("#gerritHost",seedGerritHost.toString())
未定义Object
config.seedgerrit?.host
,因此我假设必须执行if语句,但它不是

在日志中引发空指针异常:

seedGerritHost: null
seedGerritPort: null
2020-02-14 18:29:28.381+0000 [id=25]    WARNING j.util.groovy.GroovyHookScript#execute: 
Failed to run script file:/var/jenkins_home/init.groovy.d/04.jobs.groovy
java.lang.NullPointerException
    at java.lang.String.replace(String.java:2240)
    at java_lang_String$replace$0.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
    at 04_jobs.run(04.jobs.groovy:27)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:585)
    at jenkins.util.groovy.GroovyHookScript.execute(GroovyHookScript.java:136)
    at jenkins.util.groovy.GroovyHookScript.execute(GroovyHookScript.java:127)
    at jenkins.util.groovy.GroovyHookScript.run(GroovyHookScript.java:110)
    at hudson.init.impl.GroovyInitScript.init(GroovyInitScript.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at hudson.init.TaskMethodFinder.invoke(TaskMethodFinder.java:104)
    at hudson.init.TaskMethodFinder$TaskImpl.run(TaskMethodFinder.java:175)
    at org.jvnet.hudson.reactor.Reactor.runTask(Reactor.java:296)
    at jenkins.model.Jenkins$5.runTask(Jenkins.java:1119)
    at org.jvnet.hudson.reactor.Reactor$2.run(Reactor.java:214)
    at org.jvnet.hudson.reactor.Reactor$Node.run(Reactor.java:117)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

空指针异常与
.replace(“#gerritHost”,seedGerritHost)”相关
seedGerritHost
为空,因为由于某种原因未执行if语句

可能是
config.seedgerrit?.host
-literal值
“null”
我不这么认为。在其他代码中,有yaml设置文件的解析器。它可以有两种状态,一种是
config.seedgerrit?.host
设置为某个IP(作为字符串);或者根本没有价值。所以
config.seedgerrit?.host
应该设置为null,因为没有这样的值。因为在groovy 3.0.0中
null.toString()
的计算结果为
“null”
,并且
String.replace()
在第二个参数为null时引发一个NPE,NPE的可能原因是
seedGerritHost.toString()
计算结果为
null
。顺便说一句,在
if
block?
04\u jobs.run(04.jobs.groovy:27)
之后,
seedGerritHost
的值是多少。。。我认为这不是来自
if
command@Venkatesh-由于
if
语句不起作用,PrasadRanganath仍然为空