To get a trial key
fill out the form below
Team License (standard version)
Enterprise License (extended version)
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
GBP
RUB
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
>
V3105. The 'a' variable was used after …
Analyzer Diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Diagnosis of micro-optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
MISRA errors
AUTOSAR errors
Additional information
Contents

V3105. The 'a' variable was used after it was assigned through null-conditional operator. NullReferenceException is possible.

Apr 26 2021

This diagnostic warns you that a 'NullReferenceException' exception may be thrown during the program's execution. The analyzer issues a warning if you do not check a variable for 'null' and then call the variable's method or access its field or property. The key point is, the variable's value is calculated with an expression that uses the null-conditional operator.

Take a look at this example:

public int Foo (Person person)
{
  string parentName = person?.Parent.ToString();
  return parentName.Length;
}

In the code above, when initializing the 'parentName' object, we assume 'person' can be 'null'. In this case the 'ToString()' function won't be executed, and 'null' will be written to the 'parentName' variable. When the execution flow accesses the 'parentName.Length' property, a 'NullReferenceException' is thrown.

You can fix the code as follows:

public int Foo (Person person)
{
  string parentName = person?.Parent.ToString();
  return parentName?.Length ?? 0;
}

Now, if the 'parentName' variable is not 'null', the 'Foo' function returns the string length. Otherwise, it returns 0.

Here's one more example:

void DoSomething(string[] args)
{
  var nonNullStrings = args?.Where(arg => arg != null);
  foreach (var str in nonNullStrings)
    ....
}

Similar to the previous example, the 'nonNullStrings' variable initialization assumes the 'args' parameter can be 'null'. In that case, the 'nonNullStrings' variable is also assigned 'null'. When the 'foreach' loop iterates through the collection items, the 'GetEnumerator()' method is implicitly called for 'nonNullStrings'. If 'nonNullStrings' is 'null', a 'NullReferenceException' exception is thrown.

You can correct the code as follows:

void DoSomething(string[] args)
{
  var nonNullStrings =    args?.Where(arg => arg != null) 
                       ?? Enumerable.Empty<string>();

  foreach (var str in nonNullStrings)
    ....
}

This diagnostic is classified as:

You can look at examples of errors detected by the V3105 diagnostic.

This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →
Accept