I have created the connection from HttpsURLConnection to set all the parameters to it. In the end finally block i am disconnecting it as showin below. But the SonarQube build is not passing. Showing as bug while disconnecting the connection.
Proxy proxy = new Proxy(Proxy.Type.HTTP, proxnet);
HttpsURLConnection conn = (HttpsURLConnection)urlStr.openConnection(proxy);
con.set...();
finally{
conn.disconnect();
}
SonarQube addressing the Bug inside finally block with NullPointerException.
keeping a NotNull check enough to the conn object? Or is anything to be done here apart from a not-null check? I am trying in different ways.
A "NullPointerException" could be thrown; "conn" is nullable here.
Let me highlight the location of a possible exception:
Proxy proxy = new Proxy(Proxy.Type.HTTP, proxnet);
HttpsURLConnection conn;
conn = (HttpsURLConnection)urlStr.openConnection(proxy) /* EXCEPTION! variable is not assigned */;
con.set...();
finally{
conn.disconnect();
}
conn
remains set to its initial value (null
). The safe route is to add a null check in the finally block:
finally {
if (conn != null) conn.disconnect();
}