Question

I get several "Possible 'null' assignment to entity marked with 'NotNull' attribute" fingerwags from R# at code such as:

if ((webResponse.StatusCode == HttpStatusCode.OK) && (webResponse.ContentLength > 0))
{
    var reader = new StreamReader(webResponse.GetResponseStream()); // <-- R# hates this line of code

How could the StreamReader be null in this case? If it can, how can I defensively program around that possibility?

UPDATE

Okay, If I change this code:

String strResult;
WebRequest objRequest = WebRequest.Create(strURL);
WebResponse objResponse = objRequest.GetResponse();
using (var sr = new StreamReader(objResponse.GetResponseStream()))
{
    strResult = sr.ReadToEnd();
    sr.Close();
}
return strResult;

...to this:

String strResult = string.Empty;
WebRequest objRequest = WebRequest.Create(strURL);
WebResponse objResponse = objRequest.GetResponse();
using (var sr = new StreamReader(objResponse.GetResponseStream()))
{
    if (sr != null)
    {
        strResult = sr.ReadToEnd();
        sr.Close();
    }
}
return strResult;

...I still get the same fingerwag on the same ("using") line.

And if I change it to this:

String strResult = string.Empty;
WebRequest objRequest = WebRequest.Create(strURL);
WebResponse objResponse = objRequest.GetResponse();
if (objResponse != null)
{
    using (var sr = new StreamReader(objResponse.GetResponseStream()))
    {
        strResult = sr.ReadToEnd();
        sr.Close();
    }
}
return strResult;

...the "Possible 'null' assignment to entity marked with 'NotNull' attribute" still refers to that same line. So how can I nuke R#'s fingerwag? ISTM that the "using" would wrap up the necessary checking for null...

UPDATE 2

Resharper complains on this line:

var reader = new StreamReader(webResponse.GetResponseStream());

..."Possible 'null' assignment to entity marked with 'NotNull' attribute"

So I changed it to check both webResponse and reader:

var webResponse = (HttpWebResponse)webRequest.GetResponse();
if ((webResponse != null) && (webResponse.StatusCode == HttpStatusCode.OK) && (webResponse.ContentLength > 0))
{
    var reader = new StreamReader(webResponse.GetResponseStream());
    if (reader != null)
    {

...but then I get, "Expression is always true" for both of those "!=" tests; so it seems like it's saying, "Watch out! webResponse could be null!" and/or, "Watch out! reader could be null!" but then saying, "That's wasted code - they will never be null."

Was it helpful?

Solution 2

if ((webResponse.StatusCode == HttpStatusCode.OK) && (webResponse.ContentLength > 0))
{
    var responsestream = webResponse.GetResponseStream();
    if(responsestream != null) {
        var reader = new StreamReader(responsestream );
    }
}

It's not the StreamReader that's null, it's the ReponseStream. Simply add a check to make sure the returned ResponseStream is not null if you want the warning gone.

OTHER TIPS

It's not the streamreader that might be null, it's the result of webResponse.GetResponseStream()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top