JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
Java


JAVA.NULL.RET.BOOL : Return null Boolean (Java)

要旨

A method returns null instead of a java.lang.Boolean.

Methods with return type java.lang.Boolean should never return null, since these returned values are often used to call other methods or automatically unboxed into the primitive boolean type. This could lead to unexpected NullPointerException.

If null is dereferenced, Java runs into a NullPointerException. For this reason, programmers must ensure that the content of expressions dereferenced in their programs is never null.

CodeSonar provides two sets of warning classes covering various aspects of null value handling.

For the most comprehensive null pointer checking, enable both sets of warning classes: JAVA.NULL.* and JAVA.DEEPNULL.*

Strict and Non-Strict Checking

When JAVA_ANALYSIS_STRICT_MODE=No, warnings of this class will not be issued if there are indications that the possibility of a NullPointerException has been recognized and accounted for. For example, warnings will not be issued for code inside a try-catch block that explicitly catches NullPointerException, or for a JUnit test that is annotated as expecting this exception.

When JAVA_ANALYSIS_STRICT_MODE=Yes, warnings will be issued even in these cases.

プロパティ

クラス名 Return null Boolean (Java)
日本語クラス名 Return null Boolean (Java)
クラス分類 信頼性 (reliability)
ニーモニック JAVA.NULL.RET.BOOL
カテゴリー
CWE CWE:476 NULL Pointer Dereference
CERT-Java CERT-Java:EXP01-J Do not use a null in a case where an object is required
OWASP-2025 OWASP-2025:A10 Mishandling of Exceptional Conditions
対応言語 Available for Java and Kotlin.
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Return null Boolean (Java)"

//  TestReturnNull.java 
import java.util.Optional;

public class TestReturnNull {

  public Boolean returnNullForBoolean() {
    if (System.currentTimeMillis() % 2 == 0)
      return null;                       // Return null Boolean (Java) warning issued here
    else
      return Boolean.TRUE;
  }

  public Optional<String> returnNullForOptional(String value) {
    if (System.currentTimeMillis() % 2 == 0)
      return null;                       // Return null Optional (Java) warning issued here
    else
      return Optional.ofNullable(value);
  }

  public int[] returnNullForArray(int size) {
    if (System.currentTimeMillis() % 2 == 0)
      return null;                       // Return null Array (Java) warning issued here
    else
      return new int[size];
  }
}

解決法

Check if the warning corresponds to a situation where null might actually be dereferenced at runtime. If that is the case, add a nullness check for the value being dereferenced, or change the logic of the code. Sometimes, a warning of this checker corresponds to a spurious nullness check, that can be removed.

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。

 

To report problems with this documentation, please visit https://support.codesecure.com/.