When using Jenkins CLI, I get the following exception:
Exception in thread "main" java.lang.NullPointerException
at hudson.cli.CLI.flushURLConnection(CLI.java:310)
at hudson.cli.CLI.getCliTcpPort(CLI.java:285)
at hudson.cli.CLI.<init>(CLI.java:134)
at hudson.cli.CLIConnectionFactory.connect(CLIConnectionFactory.java:72)
at hudson.cli.CLI._main(CLI.java:469)
at hudson.cli.CLI.main(CLI.java:384)
The code where the exception is thrown looks like this:
299 private void flushURLConnection(URLConnection conn) { 300 byte[] buf = new byte[1024]; 301 try { 302 InputStream is = conn.getInputStream(); 303 while (is.read(buf) >= 0) { 304 // Ignore 305 } 306 is.close(); 307 } catch (IOException e) { 308 try { 309 InputStream es = ((HttpURLConnection)conn).getErrorStream(); 310 while (es.read(buf) >= 0) { 311 // Ignore 312 } 313 es.close(); 314 } catch (IOException ex) { 315 // Ignore 316 } 317 } 318 }
It seems that es is null, the documentation for getErrorStream states:
public InputStream getErrorStream() Returns: an error stream if any, null if there have been no errors, the connection is not connected or the server sent no useful data.
A solution is just to add a check for null before reading from the stream. But maybe there is some deeper problem here because the NullPointerException occurs in a catch block of an IOException. But output from both the input stream and error stream is ignored so perhaps it is nothing to worry about?
The CLI is used from jobs in Jenkins, so this cause builds to fail. It is only on some nodes this is happening so the root cause is probably an environment issue because using the CLI has worked before.