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
C#


CSHARP.NULL.DEREF : Null Pointer Dereference (C#)

Summary

A null pointer dereference, including the following.

Corresponding deep warning class: CSHARP.DEEPNULL.DEREF.

If the null value gets dereferenced, C# runs into a NullReferenceException. For this reason, programmers must ensure that the content of expressions dereferenced in their programs is never null. Solving this problem is in general hard. This checker provides a coverage of the most frequent scenarios when null might end up being dereferenced. For a sound alternative to this checker, that covers all possible situations, see the Nullness checker. However, BasicNullness is much faster than Nullness and issues a more restricted set of false alarms, hence it is often the best solution for a rapid identification of the most frequent null-pointer errors in a program.

Strict and Non-Strict Checking

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

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

Properties

Class Name Null Pointer Dereference (C#)
Significance reliability
Mnemonic CSHARP.NULL.DEREF
Categories
CWE CWE:456 Missing Initialization of a Variable
  CWE:476 NULL Pointer Dereference
OWASP-2025 OWASP-2025:A10 Mishandling of Exceptional Conditions
Availability Available for C# only.
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 (C#)"

Examples

Example 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

        public String getField()
        {
            return field;
        }

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

    }

    public class TestGetFieldFromNullWarning
    {
        public bool condition;

        public void getFieldFromNullWarning()
        {
            Bean bean = null;

            if (condition)
                bean = new Bean();

            if (bean.getField().Equals("")) // Null Pointer Dereference (C#) warning issued here
                                            // - receiver bean of getField() is never checked for nullness
            {
                Console.WriteLine("Field is empty");
            }
        }

    }
}
Example 2

Look for variables which value is always null

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestVariableCanOnlyBeNullWarning
{
    public class TestVariableCanOnlyBeNullWarning
    {

        public bool variableCanOnlyBeNullWarning()
        {
            int[] var = null;

            if (var == null) // Null Pointer Dereference (C#) warning issued here 
                             // - variable 'var' is always null at this point
                return false;

            for (int i = 0; i < var.Length; i++)
                Console.WriteLine("pippo");

            return true;
        }

    }
}
Example 3
using System;

public class MyMainClass 
{

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

    }

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

}
Example 4
using System;

public class MyExceptionHandler 
{
    
    public void Handle(Exception e)
    {

        if (e != null)
        {
            Console.WriteLine("Exception found: " + e.GetType());
        }
        
        throw e; // Null Pointer Dereference (C#) warning issued here 
                 // - e may be null 
     }

}
Example 5
public class CountingStars 
{

      int stars;
      
      object _lock;
      
      public void add(int value)
      {

            lock(_lock)  // Null Pointer Dereference (C#) warning issued here
                         // - synchronizing on null value
            {
               this.stars += value;   
            }
      }
}

The programmer should initialize the _lock object as follows:

public class CountingStars 
{

      int stars;
      
      readonly object lock = new object();
      
      public void Add(int value)
      {

            lock(_lock)
            {
               this.stars += value;   
            }
      }
}
Example 6
public class ArrayProgram
{

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

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

        public static void Store()
        {
            int[] arr3 = null;
            arr3[0] = 3;                   // "Null Pointer Dereference (C#)" warning issued here
        }
}
Example 7
using System;

namespace DocumentationExamples
{

    public class BasicNullness
    {

        public static void Main(string[] args)
        { }

        string f = null;
        public void Test1(string s)
        {
            if ((DateTime.Now.Millisecond % 2) == 0 || f != null)
                Console.WriteLine(f.Length);     // Null Pointer Dereference (C#) warning issued here 
                                                 //  - f is always null 
            if (s != null)
                Console.WriteLine("s01 is null");
            Console.WriteLine(s.Length);         // Null Pointer Dereference (C#) warning issued here 
                                                 // - Either the previous null-check of s is redundant (and should be removed)
                                                 //   or there should be one for this call too. 
        }
        public void Test2(string par)
        {
            string s = DateTime.Now.Millisecond % 2 == 0 ? null : "ciao";
            BasicNullnessSupport ts = new BasicNullnessSupport();
            int n01 = ts.M01(s);                 // Two Null Parameter Dereference (C#) warning instances issued here 
                                                 // - one because s may have been assigned null 
                                                 // - one because there is a subsequent null-check of s: either that check is redundant (and should be removed)
                                                 //    or there should be one for this call too. 
            int n02 = ts.M02(s);
            if (s != null)
                ts.M01(s);
            ts.M01(par);
            Console.WriteLine(n01 + n02);
        }
        public void Test3(string par)
        {
            if (par != null)
                Console.WriteLine("is non-null");
            new BasicNullnessSupport().M01(par); // Null Parameter Dereference (C#) warning issued here
                                                 // - either the previous null-check of s is redundant (and should be removed)
                                                 //   or there should be one for this call too.
        }
    }
    
    public class BasicNullnessSupport
    {
        public int M01(string s)
        {
            return s.Length;
        }
        public int M02(string s)
        {
            if (s != null)
                return s.Length;
            return -1;
        }
    }
}

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/.