Java


JAVA.NULL.PARAM.ACTUAL : Null Parameter Dereference (Java)

要旨

An actual parameter passed to a method might be null.

In Java, the classes java.lang.reflect.Method and java.lang.reflect.Field have some methods which throw a NullPointerException, if a specified argument is null and the method/field (used through reflection) is an instance method/field. When this instructions are detected, CodeSonar tries to statically resolve the reflection, in order to check if it is a valid reflection or if it leads to a NullPointerException. For performance reasons, CodeSonar checks only the constants inside the method where the reflection instruction is called.

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.

プロパティ

クラス名 Null Parameter Dereference (Java)
日本語クラス名 Null Parameter Dereference (Java)
クラス分類 信頼性 (reliability)
ニーモニック JAVA.NULL.PARAM.ACTUAL
カテゴリー
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
対応言語 Java で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Null Parameter Dereference (Java)"

// TestSupport.java
public class TestSupport {

  public int m01(String s) {
    return s.length();
  }

  public int m02(String s) {
    if (s != null)
      return s.length();

    return -1;
  }
}
// Test.java
public class Test {
  String f = null;

  public void test1(String s) {
    if ((System.nanoTime() % 2) == 0 || f != null)
      System.out.println(f.length()); // Null Pointer Dereference (Java) warning issued here 

    if (s != null)
      System.out.println("s01 is null");

    System.out.println(s.length());   // Null Pointer Dereference (Java) warning issued here 
  }

  public void test2(String par) {
    String s = System.currentTimeMillis() % 2 == 0 ? null : "ciao";
    TestSupport ts = new TestSupport();
    int n01 = ts.m01(s);              // Null Parameter Dereference (Java) warning issued here 
    int n02 = ts.m02(s);
                
    if (s != null)
      ts.m01(s);

    ts.m01(par);

    System.out.println(n01 + n02);
  }

  public void test3(String par) {
    if (par != null)
      System.out.println("is non-null");

    new TestSupport().m01(par);       // Null Parameter Dereference (Java) warning issued here 
  }
}
//  ReflectionExample.java 
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class ReflectionExample {
    public static void Method() throws NoSuchFieldException, SecurityException,
                                       ClassNotFoundException, InstantiationException,
                                       IllegalAccessException, IllegalArgumentException,
                                       InvocationTargetException {
            
      Class<?> c = Class.forName(ClassUsedByReflection.class.getName());

      Field success1 = c.getField("ok");
      success1.get(nil());

      Field success2 = c.getField("hi");
      success2.get(new Object());              // arg0 is not null

      Field fail1 = c.getField("hi");
      fail1.get(nil());     // "Null Parameter Dereference (Java)" warning issued here. 

      Field[] fail2 = c.getFields();
      for(Field f : fail2)
          f.get(nil());     // "Null Parameter Dereference (Java)" warning issued here: 
                            // the ClassUsedByReflection class contains at least one instance field.      
    }

    private static class ClassUsedByReflection{
      public String hi = "Hello World!";       // instance field 
      public static String ok = "I am ok!";    // static, so not an instance field
      public ClassUsedByReflection(){ }
    }       

    public static String nil() {
        return null;
    }
}

解決法

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.

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

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