ReSharper: how to remove the warning "Possible" System.NullReferenceException "

Here is a piece of code:

IUser user = managerUser.GetUserById(UserId); if ( user==null ) throw new Exception(...); Quote quote = new Quote(user.FullName, user.Email); 

Everything is fine here. But if I replace the string "if" with the following:

 ComponentException<MyUserManagerException>.FailIfTrue(user == null, "Can't find user with Id=" + UserId); 

where the implementation of the function is as follows:

 public abstract class ComponentException<T> : ComponentException where T : ComponentException, new() { public static void FailIfTrue(bool expression, string message) { if (expression) { T t = new T(); t.SetErrorMessage(message); throw t; } } //... } 

Then ReSharper generates a warning to me: perhaps a "System.NullReferenceException" indicating the 1st use of the "user" object.

Q1. Why does this raise such an exception? As far as I see, if user==null exception will be thrown and execution will never reach the point of use.

Q2. How to remove this warning? Please note: 1. I do not want to suppress this warning with comments (I will have many similar parts and I do not want to turn my source code into 'commented garbage); 2. I do not want to change the ReSharper settings in order to change this problem from a warning to a โ€œsuggestionโ€ in a โ€œpromptโ€.

Thank you

Any thoughts are welcome!

PS I am using Resharper 5.1, MVSV 2008, C #

+11
c # nullreferenceexception configuration resharper
source share
6 answers

Q1: Because Resharper does not analyze the path. It just sees a possible null link and flags.

Q2: You cannot help but do what you have already provided.

+6
source share

Resharper considers only the current method for its analysis and does not recursively analyze other methods that you call.

However, you can redirect Resharper and give it meta-information about some methods. He knows, for example, about "Assert.IsNotNull (a)" and will take this information into account for analysis. You can create an external annotation file for Resharper and provide it with additional information about some library to make its analysis better. Perhaps this may offer you a way to solve your problem.

More information can be found here .

An example showing how it is used for the Microsoft.Contracts library can be found here .

+10
source share

New reply in old mail ...

Here is a small example of my code regarding using CodeContract through ContractAnnotation with Resharper:

  [ContractAnnotation("value:null=>true")] public static bool IsNullOrEmpty(this string value) { return string.IsNullOrEmpty(value); } 

It is very simple ... if you find breading in the woods. You can also check other cases.

A good day

+6
source share

You know (or expect) that this code will throw an exception if there is a null reference:

 ComponentException<MyUserManagerException>.FailIfTrue([...]); 

However, since there is no contract, specifying this, ReSharper should assume that this is a normal method call that can return without any exception in any case.

Make this method an implementation of the ReSharper contract or as a simple workaround (which affects only debug mode, therefore does not affect performance for release mode) immediately after calling FailIfTrue :

 Debug.Assert(user != null); 

This will save you from a warning, and as an added bonus, check the runtime in debug mode to make sure that the condition that you accepted after calling FailIfTrue is indeed fulfilled.

+3
source share

This is caused by the Resharper engine. This "possible NullReferenceException" occurs because someone (possibly in Resharper) has declared / configured somewhere an annotation of the method.

Here's how it works: ReSharper NullReferenceException Analysis and its contracts

Unfortunately, sometimes this useful annotation is simply incorrect.

If an error is detected, you should report it to JetBrains and they will update annotations in the next version. They are used to it.

Meanwhile, you can try to fix it yourself. Read more in the article :)

0
source share

Please check if you have any user == zero if validation is above the specified code. If there is, then ReSharper considers that the variable "may be null", therefore, it recommends that you use a check / statement before referring to it. In some cases, this is the only way ReSharper can guess whether a variable can be null or not.

0
source share

All Articles