[英]SonarQube A NullPointerException could be thrown; Conn is nullable here
I have created the connection from HttpsURLConnection to set all the parameters to it.我已经从 HttpsURLConnection 创建连接以设置所有参数。 In the end finally block i am disconnecting it as showin below.
最后终于阻止我断开它,如下所示。 But the SonarQube build is not passing.
但是 SonarQube 构建没有通过。 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. SonarQube 使用 NullPointerException 解决 finally 块中的 Bug。
keeping a NotNull check enough to the conn object?对 conn object 进行足够的 NotNull 检查? Or is anything to be done here apart from a not-null check?
或者除了非空检查之外还有什么要做的吗? I am trying in different ways.
我正在尝试不同的方式。
A "NullPointerException" could be thrown;可能会抛出“NullPointerException”; "conn" is nullable here.
“conn”在这里可以为空。
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
). conn
仍设置为其初始值 ( null
)。 The safe route is to add a null check in the finally block:安全的路线是在 finally 块中添加一个 null 检查:
finally {
if (conn != null) conn.disconnect();
}
when you put initiation on (HttpsURLConnection)urlStr.openConnection(proxy);
当你启动
(HttpsURLConnection)urlStr.openConnection(proxy);
it is possible to have an exception and don't initiate conn also finally will be run at any situation so sonar predicate a nullPointerException.可能有异常并且不启动 conn 也最终将在任何情况下运行,因此声纳谓词 nullPointerException。 maybe you need check
conn != null
before calling conn.disconnect();
也许您需要在调用
conn.disconnect();
之前检查conn != null
; in finally.在最后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.