Possible null reference exception

Hi,

I'm just starting to eval RS6.0...

On of the first things I stumbled on, when checking my code with RS 6.0 is a "possible null pointer reference" hint in a method, although the parameter is checked to be not null. Please see my code:

private ItemResponse<ItemResult> SampleMethod(ItemRequest request)
{
if (request == null)
{
  throw new ArgumentNullException("request");
}
...
Item = new ItemResult
{
  Request = request.SomeString,  // <== here RS markes "request" as "possible null pointer ref"
  ...
}
}

So RS does not "know" that I already made sure that "request" cannot be null in my code.

Is this a "bug" or is RS just not able to recognize this?

Thanks,

Stefan

0
5 comments
Avatar
Permanently deleted user

Hello Stefan,

Could you please attach a full sample solution demonstrating this behavior?
Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

Hi,

I'm just starting to eval RS6.0...

On of the first things I stumbled on, when checking my code with RS
6.0 is a "possible null pointer reference" hint in a method, although
the parameter is checked to be not null. Please see my code:

private ItemResponse<ItemResult> SampleMethod(ItemRequest request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
...
Item = new ItemResult
{
Request = request.SomeString,  // <== here RS markes "request" as
"possible null pointer ref"
...
}
}
So RS does not "know" that I already made sure that "request" cannot
be null in my code. Is this a "bug" or is RS just not able to
recognize this?

Thanks,
Stefan
---
Original message URL:
http://devnet.jetbrains.net/message/5325861#5325861



0
Avatar
Permanently deleted user

I cannot duplicate your error. Can you give us more code so we can see what we can do? I created empty classes and I don't get the invalid hint.

0
Avatar
Permanently deleted user

Hi Andrey,

thanks for your immediate feedback.
Please find attached a sample that demonstrates the behaviour.

Stefan



Attachment(s):
RS6PossibleNullRefSample.zip
0

Your sample makes it quite obvious why R# is flagging this:

try
{
     if (ratingRequest == null)
     {
          throw new ArgumentNullException("ratingRequest");
     }
     ...
}
catch (Exception ex)
{
     ...
     Request = ratingRequest.SoapRequest,
     ...
}


If your ratingRequest argument is null, you'll throw an ArgumentNullException from within the try..catch block. Your "catch (Exception)" block will then catch the exception, and attempt to access the SoapRequest property of the ratingRequest argument, which will throw a NullReferenceExecption.

To fix the problem, move your argument test outside of the try..catch block:

if (ratingRequest == null)
{
     throw new ArgumentNullException("ratingRequest");
}
try
{
     ...
}
catch (Exception ex)
{
     ...
}

0
Avatar
Permanently deleted user

Richard,

thanks for pointing this out. It should have been obvious to me :(

I solved it using a ternary operator (:?) on the statement marked, as I cannot move the argument check out of the try-block as this part of the code is running as a service. I need to get the exception back in my status message.

Thanks again,
Stefan

0

Please sign in to leave a comment.