0

I have a Tuple that contains HTTP headers and their values. This portion of my code is using the Content-Encoding: header to work out if the content is compressed with gzip or deflate.

var contentEncoding = responseHeaders.Find(p => p.Item1.ToLower() == "content-encoding");

// decompress the content if needed
if (!String.IsNullOrEmpty(contentEncoding.Item2))
    if (contentEncoding.Item2.ToLower() == "gzip")
        bodyPlain = Tools.Gunzip(ret);
    else if (contentEncoding.Item2.ToLower() == "deflate")
        bodyPlain = Tools.Decompress(ret);
    else
        bodyPlain = UTF8Encoding.UTF8.GetString(ret);
else
    bodyPlain = UTF8Encoding.UTF8.GetString(ret);

My problem is that if responseHeaders.Find does not return a result, then contentEncoding.Item2 does not exist and I get

Object reference not set to an instance of an object.

I tried checking contentEncoding.Item2 with String.IsNullOrEmpty and !=null, but it still returns the above error.

I also tried specifying contentEncoding before using it, like this:

var contentEncoding = new Tuple<string,string>("","");
contentEncoding = responseHeaders.Find(p => p.Item1.ToLower() == "content-encoding");

But still get the same error.

What am I doing wrong here, and is there a better way of achieving this?

James
  • 656
  • 2
  • 10
  • 24
  • You need to check whether `contentEncoding` is `null`. It looks like `Find()` is returning `null` when it doesn't find a match. – JLRishe Jan 12 '20 at 12:28
  • goddamit, I knew it was something stupid, but that takes the biscuit. Thanks – James Jan 12 '20 at 12:30
  • Do I still need to check contentEncoding.Item2 for null as well as contentEncoding? There is a possibility the header could be "Content-Encoding:" with no value? – James Jan 12 '20 at 12:34
  • 1
    You could use the null coalescing operator (`?.`) in your condition: `if (!String.IsNullOrEmpty(contentEncoding?.Item2))` – Bill Tür stands with Ukraine Jan 12 '20 at 12:41
  • Thanks, maybe I can use that and simplify everything with a switch statement – James Jan 12 '20 at 12:53
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Pavel Anikhouski Jan 12 '20 at 14:08
  • Why don't you use the automatic decompression feature? – Jimi Jan 12 '20 at 14:12

1 Answers1

0

This seems to be working out better for me. Thanks to Thomas Schremser for the idea of the coalescing operator:

                    switch (contentEncoding?.Item2.ToLower() ?? String.Empty)
                    {
                        case "":
                            bodyPlain = UTF8Encoding.UTF8.GetString(ret);
                            break;
                        case "gzip":
                            bodyPlain = Tools.Gunzip(ret);
                            break;
                        case "deflate":
                            bodyPlain = Tools.Decompress(ret);
                            break;
                    }
James
  • 656
  • 2
  • 10
  • 24