[英]Sonar Cube “null pointer exception could be thrown” : False positive?
我在以下代碼中有聲納立方體提出的錯誤:
private request = null;
try
{
request = createRequest(); // create Request
log.info(" Request created with details "+request.toString());
}
catch(...)
{
}
catch(Exception e)
{
}
該錯誤在 log.info 語句中提出,因為它建議在使用前檢查 NULL 的請求。 But my doubt is in case I check it for null and if it is actually null, then I would like it to goto catch exception block, which anyhow it will go in case I dont check explicitly for NULL. 那么它是誤報嗎?我該如何處理?
我不認為這是一個誤報。 request
可以是代碼中的 null ,因此在其上調用toString
將導致拋出 null 指針異常。
如果createRequest
可以返回 null 那么你應該明確地檢查它而不是僅僅依靠日志語句。 類似於以下內容。
private request = null;
try
{
request = createRequest(); // create Request
if ( null == request ){
throw new NullRequestException();
}
log.info(" Request created with details "+request.toString());
}
catch(...)
{
}
catch(Exception e)
{
}
在旁注中,我發現像您在代碼段中所做的那樣捕獲Exception
通常是一個壞主意。 我看不到足夠的代碼上下文,無法知道在您的情況下是否屬實,但您可能應該看看它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.