[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
  System Requirements
  Improve Performance
 [Expand]Features by Language
 [Expand]Getting Started
 [Expand]How To
 [Expand]Coding Assistance
 [Expand]Navigation and Search Tools
 [Collapse]Static Code Analysis
  [Collapse]Analyzers Library
    CRR0001 - Redundant sub-expressions in a binary operation
    CRR0002 - Suspect variable reference in a For-loop condition
    CRR0003 - Suspect variable reference in the For-loop iterator section
    CRR0004 - Ternary expression has identical branches
    CRR0005 - Expression value is always the same
    CRR0006 - Suspect variable reference in null-check following as-cast
    CRR0007 - String format item/argument mismatch
    CRR0008 - If-block matches else-block
    CRR0009 - Subsequent if-statements have identical conditions
    CRR0010 - Exception without throw
    CRR0011 - Next if-statement has an identical condition that will never be reached
    CRR0012 - Logical OR expression has opposite operands
    CRR0013 - The member always returns the same value
    CRR0014 - The variable is assigned twice in succession
    CRR0015 - Logical OR expression redundancy
    CRR0016 - Method call's return value is ignored
    CRR0017 - Null check follows usage
    CRR0018 - Suspect assignment reversal
    CRR0019 - Expression contains redundant subsets
    CRR0020 - Integral divide operation cast to float
    CRR0021 - Subsequent else-if conditions are identical
    CRR0022 - Unreachable conditional code block (a similar condition in the else branch)
    CRR0023 - Unnecessary conditional
    CRR0024 - Increase precision with a built-in constant or call
    CRR0025 - Unreachable conditional code block (the inversed condition is already satisfied)
    CRR0026 - Unused member
    CRR0027 - Possible System.NullReferenceException
    CRR0028 - Empty event handler
    CRR0029 - The ConfigureAwait(true) is called implicitly
    CRR0030 - Redundant 'await'
    CRR0031 - The returned Task is null
    CRR0033 - The void async method should be in a try/catch block
    CRR0034 - The async method should contain the
    CRR0035 - No CancellationToken parameter in the async method
    CRR0036 - The 'await Task.FromResult()' expression is redundant
    CRR0037 - Task.Wait is used in an async method
    CRR0038 - The CancellationToken parameter is never used
    CRR0039 - The 'await' expression without cancellation token
    CRR0041 - Use Task.Run instead of Task.Factory.StartNew
    CRR0042 - Unused parameter
    CRR0043 - Unused type
    CRR0044 - Unused local variable
    CRR0045 - Local variable can be replaced with discard
    CRR0046 - Redundant field initialization
    CRR0047 - The type can be moved to a separate file
    CRR0048 - The type name does not correspond to the file name
    CRR0049 - Environment.NewLine can be used
    CRR0050 - String.Compare can be used
    CRR0051 - String.IsNullOrEmpty can be used
    CRR0052 - String interpolation can be used
   Code Metrics
   Spell Checker
 [Expand]Visualization Tools
 [Expand]Code Style Assistance
  Building Assistance
  Debugging Assistance
 [Expand]Refactoring Assistance
 [Expand]Unit Testing Assistance
 [Expand]Decompiling Tools
 [Expand]Configuration Options
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

CRR0027 - Possible System.NullReferenceException

This analyzer detects unprotected code that can potentially raise a System.NullReferenceException exception.

CodeRush checks the following elements: member's qualifiers, collections inside of a foreach loop, collections used to call indexers.

Expanded How to Enable

Choose the Possible System.NullReferenceException issue in the Editor | Language | Code Analysis | Code Issues Catalog options page and check the Enabled checkbox.

Expanded Null Reference Analysis Modes

CodeRush can analyze values in Trusting, Skeptical, and Thorough diagnostic modes. You can specify the Possible System.NullReferenceException diagnostic mode for methods, properties, parameters, fields, and local variables.

The following sections describe each diagnostic mode in details.


If an element is not checked for null in the current scope and does not have the [CanBeNull] attribute, the analyzer trusts this element and does not show the diagnostic.

In the example below, the SortElements method does not provide information whether Elements are null or not null. The analyzer considers Elements as not null in Trusting mode and does not show the diagnostic in the Code Issues window.

The analyzer shows the diagnostic in Trusting mode, for example, if the following code has a null check error:

You can mark the Elements property with the [CanBeNull] code annotation attribute to indicate this property can be null. In this case, the analyzer shows the diagnostic even if the code does not have a null check expression.


If an element is not checked for null in the current scope, the analyzer does not show the diagnostic only for elements that have the [NotNull] attribute.

In the example below, the analyzer shows the diagnostic for the Elements property because this property is not checked for null.

If you want the analyzer does not show this diagnostic, add the null check.

You can also use the [NotNull] code attribute to indicate the analyzer the Elements property is not null.


If an element is not checked for null in the current scope and the element does not have the [CanBeNull] or [NotNull] attribute, the analyzer checks if the element can never be null (lazy initialized, returns new created object, etc). The mode works more slowly than others.

In the following code, the analyzer shows the diagnostic in Skeptical mode and does not show in Thorough mode. This happens because the Elements property always returns a not null value.

Expanded Annotation Attributes

CodeRush checks annotation attributes by a partial name. This means you do not need to reference annotations, just declare them in code.

Expanded Customization

You can configure the Possible System.NullReferenceException diagnostic' settings in the Editor | Language | Code Analysis | Possible NullReferenceException options page. The following screenshot shows the default settings.

CodeRush also provides an option to check anonymous methods' parameters.

Is this topic helpful?​​​​​​​