简体   繁体   English

SonarQube 可能抛出 NullPointerException; Conn 在这里可以为空

[英]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.

相关问题 可能会抛出“NullPointerException”; “上下文”在这里可以为空 - A "NullPointerException" could be thrown; "context" is nullable here SonarQube Blocker问题NullPointerException可能会因为“联系人”在此处为空而抛出 - SonarQube Blocker Issue NullPointerException might be thrown as 'contacts' is nullable here SonarQube - 可能会抛出“NullPointerException”; - SonarQube - A “NullPointerException” could be thrown; NullPointerException可能会因为“值”为空而在此处发出声纳警告 - NullPointerException might be thrown as 'value' is nullable here sonar warning Sonarqube说:可能会抛出“ NullPointerException”。 “ getFolderPath()”可以返回null - Sonarqube says: A “NullPointerException” could be thrown; “getFolderPath()” can return null 为什么将NullPointerException抛出在这里? - Why the NullPointerException will be thrown here? 我该如何解决squid:S2259`NullPointerException可能会被抛出,因为'getString'在这里可以为空。 - How do I fix squid:S2259 `NullPointerException might be thrown as 'getString' is nullable here`? SonarQube Java if语句中可能抛出“ NullPointerException” - SonarQube Java 'NullPointerException might be thrown' in if statement 可能会抛出“NullPointerException”; “getBody”可以返回 null 声纳 - A “NullPointerException” could be thrown; “getBody” can return null Sonar conn = this.jdbcFactory.getConnection() 中的 NullPointerException; - NullPointerException in conn = this.jdbcFactory.getConnection();
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM