papanito
papanito

Reputation: 2574

Jenkins library: java.lang.NullPointerException: Cannot invoke method xxx on null object

I am using declarative pipelines and separate pipline helpers. In one of the helper I have the file vars/getTriggerCause.groovy with

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

Now I want to use the function in the Jenkisfile like this

echo getTriggerCause().isTime()

Which results in an NPE:

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

When I look at this I would expect this works. The only difference to the linked example is that I load the library dynamically from scm.

Upvotes: 2

Views: 7186

Answers (2)

Mahesh
Mahesh

Reputation: 1683

I had a similar error message when i was using load , problem was i had forgotten to return this from the groovy file.

Upvotes: 8

papanito
papanito

Reputation: 2574

Removing the parentheses solve the problem so this works

getTriggerCause.isTime()

Upvotes: 0

Related Questions