小编典典

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

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

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

echo getTriggerCause().isTime()

这导致了NPE:

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

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


阅读 367

收藏
2020-07-25

共1个答案

小编典典

删除括号即可解决问题,因此可以正常工作

getTriggerCause.isTime()
2020-07-25