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.DEREF : Null Pointer Dereference (Java)

Summary

A null pointer dereference, including the following.

Corresponding deep warning class: JAVA.DEEPNULL.DEREF.

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.

Properties

Class Name Null Pointer Dereference (Java)
Significance reliability
Mnemonic JAVA.NULL.DEREF
Categories
CWE CWE:456 Missing Initialization of a Variable
  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
Availability Available for Java and Kotlin.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Null Pointer Dereference (Java)"

Examples

Operations on null arrays

// ArrayProgram.java
public class ArrayProgram {

    public static void Length() {
        int[] arr1 = null;
        int arrayLength = arr1.length; // "Null Pointer Dereference (Java)" warning issued here"
    }

    public static void Load() {
        int[] arr2 = null;
        int n = arr2[0];               // "Null Pointer Dereference (Java)" warning issued here"
    }

    public static void Store() {
        int[] arr3 = null;
        arr3[0] = 3;                   // "Null Pointer Dereference (Java)" warning issued here"
    }
}

Synchronization on null

// CountingStars.java
public class CountingStars {
    int stars;
    Object lock;

    public void add(int value){
        synchronized(lock){ // "Null Pointer Dereference (Java)" warning issued here
            this.stars += value;   
        }
    }
}

To avoid the NullPointerException caused by attempting to synchronize on null, initialize the lock object as follows.

// CountingStars.java, after modification
public class CountingStars {
    int stars;
    final Object lock = new Object();
      
    public void add(int value){
        synchronized(lock){
            this.stars += value;   
        }
    }
}

Throw command that throws null

// MyExceptionHandler.java
public class MyExceptionHandler {
    
    public void handle(Exception e) throws Exception {
        if(e != null ) {
            System.out.println("Exception found: " + e.getClass());
        }
        throw e; // "Null Pointer Dereference (Java)" warning issued here
    }
}

Writing a field into a null receiver

// MyMainClass.java
public class MyMainClass {

    public static void main(String[] args) {
        Place p = null;
        p.h = "These's no place like home!"; // "Null Pointer Dereference (Java)" warning issued here 

    }

    private static class Place {
        
        public String h;
        
        // ...
    }
}

Method call on a null receiver

// TestGetFieldFromNullWarning.java
package test;

public class TestGetFieldFromNullWarning {

    public boolean condition;

    public class Bean {
        private String field = "value";

        public String getField() {
            return field;
        }

        public void setField(String field) {
            this.field = field;
        }
    }

    public void getFieldFromNullWarning(){
        Bean bean = null;
        
        if(condition)
            bean = new Bean();
        
        if(bean.getField().equals("")) { // "Null Pointer Dereference (Java)" warning issued here
            System.out.println("Field is empty");
        }
    }
}

Variables whose value is always null

Highlighting variables which value is always null may help discovering dead code or simply logic that is no more required.

// TestVariableCanOnlyBeNullWarning.java
package test;

public class TestVariableCanOnlyBeNullWarning {
    
    @SuppressWarnings("unused")
    public boolean variableCanOnlyBeNullWarning(){
        int[] var = null;

        if (var == null)  //  "Null Pointer Dereference (Java)" warning issued here
            return false;

        for (int i = 0; i < var.length; i++)
            System.out.print("pippo");

        return true;
    }
}

Resolution

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.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

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