'How to check NPE in a try() segment for autocloseable?

When I use 'try(){} catch' to deal with autoclosable object, findbugs report a bug that I don't check nullale for the instance populate in the '()' segment. So how to check if the instance in '()' is not null? I don't want to move the instance out of '()' to check if it is null. My code segment looks like this, try(CloseableHttpClient httpClient = httpClientBuilder.build()); CloseableHttpResponse response = httpClient.execute(request)) { blabla... } catch (Exception e) { blabla... } How to check httpClient is not null in a graceful way? And if get a null instance, how to deal with it here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source