jenkinsci / jenkinsci/java-client-api

NullPointerException after calling triggerJobAndWaitUntilFinished

Open
#440 4 comments 5 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
913
Forks
466
PR merge metrics
No merged PRs in 30d

Description

When I call `triggerJobAndWaitUntilFinished()` I get an NPE:

```
java.lang.NullPointerException: null
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:878) ~[guava-28.1-jre.jar:na]
at com.google.common.net.PercentEscaper.escape(PercentEscaper.java:145) ~[guava-28.1-jre.jar:na]
at com.offbytwo.jenkins.model.Job$MapEntryToQueryStringPair.apply(Job.java:185) ~[jenkins-client-0.3.8.jar:0.3.8]
at com.offbytwo.jenkins.model.Job$MapEntryToQueryStringPair.apply(Job.java:181) ~[jenkins-client-0.3.8.jar:0.3.8]
at com.google.common.collect.Iterators$6.transform(Iterators.java:783) ~[guava-28.1-jre.jar:na]
at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47) ~[guava-28.1-jre.jar:na]
at org.apache.commons.lang.StringUtils.join(StringUtils.java:2831) ~[commons-lang-2.3.jar:2.3]
at org.apache.commons.lang.StringUtils.join(StringUtils.java:2878) ~[commons-lang-2.3.jar:2.3]
at com.offbytwo.jenkins.model.Job.build(Job.java:150) ~[jenkins-client-0.3.8.jar:0.3.8]
at com.offbytwo.jenkins.model.Job.build(Job.java:125) ~[jenkins-client-0.3.8.jar:0.3.8]
at com.offbytwo.jenkins.JenkinsTriggerHelper.triggerJobAndWaitUntilFinished(JenkinsTriggerHelper.java:88) ~[jenkins-client-0.3.8.jar:0.3.8]
...
```
The stacktrace was truncated because the other lines only show my code.

In my code I basically do this:

Map params = new HashMap<>();
params.put("identifier", command.getIdentifier());
...
BuildWithDetails details = null;
try {
details = th.triggerJobAndWaitUntilFinished(jp.getJob(), params, true);
} catch (HttpResponseException e) {
log.warn("You were denied by Jenkins. Please check user and password used", e);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (NullPointerException e) {
log.warn("It looks like calling job {} failed. Value of details: {}", jp.getJob(), details, e);
}

I already forced the use of a current version of Guava because by default v17 was chosen. After checking the `params` I found out that in some situations one of the parameters turns `null`. And this causes the library to fail.

Is it possible to have the library return a descriptive error message in such situations ?

But even after I fixed this and made sure only to call the method when no null values are inside the map, the call fails with another NPE:

```
java.lang.NullPointerException: null
at com.offbytwo.jenkins.JenkinsServer.getBuild(JenkinsServer.java:824) ~[jenkins-client-0.3.8.jar:0.3.8]
at com.offbytwo.jenkins.JenkinsTriggerHelper.triggerJobAndWaitUntilFinished(JenkinsTriggerHelper.java:178) ~[jenkins-client-0.3.8.jar:0.3.8]
at com.offbytwo.jenkins.JenkinsTriggerHelper.triggerJobAndWaitUntilFinished(JenkinsTriggerHelper.java:90) ~[jenkins-client-0.3.8.jar:0.3.8]
...
```

I tried to debug this but all I could find out is that here the `queueItem` becomes `null`:

![image](https://user-images.githubusercontent.com/16045549/69827711-d4631600-1218-11ea-91ff-6189013ee671.png)

When checking the Jenkins job I can see that it was created (it failed with exit code 6 due to some other reasons, but it was created and ran)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.