Jenkins 库:java.lang.NullPointerException:无法在空对象上调用方法 xxx

pap*_*ito 0 jenkins

我正在使用声明性管道和单独的管道助手。在助手之一,我有一个文件vars/getTriggerCause.groovy

/**
* Checks for cause of the job trigger and returns respective cause
* @return user, scm, time or other
*/
def String getCause() {
    echo "CAUSE ${currentBuild.rawBuild.getCauses().properties}"
    def cause = "${currentBuild.rawBuild.getCauses()}"
     if (cause =~ "UserIdCause") {
         return "user"
    }
}

/**
* Checks if trigger cause of the job is the timer
* @return true if trigger is timer
*/
def boolean isTime() {
    return this.call() == "time"
}
Run Code Online (Sandbox Code Playgroud)

现在我想像这样使用 Jenkisfile 中的函数

echo getTriggerCause().isTime()
Run Code Online (Sandbox Code Playgroud)

这会导致 NPE:

java.lang.NullPointerException: Cannot invoke method getCause() on null object
Run Code Online (Sandbox Code Playgroud)

当我看到这个时,我希望这有效。与链接示例的唯一区别是我从 scm 动态加载库。

Mah*_*esh 5

我在使用 load 时收到了类似的错误消息,问题是我忘记this从 groovy 文件中返回。