0

I have read several articles and questions on Stack Overflow and still cannot see what I am doing wrong. Not using C#6 -- sorry for not posting that at first. VS 2013.

This code works:

if (row.Cells["CSR_Notes"].Value != null)
                    {
                    deliveryEvent.CSR_Notes = row.Cells["CSR_Notes"].Value.ToString();
                    }

and this code works:

 deliveryEvent.CSR_Notes = row.Cells["CSR_Notes"].Value != null 
                        ? row.Cells["CSR_Notes"].Value.ToString() : "";

But this code throws a "Object reference not set..." error if the value is null.

 deliveryEvent.CSR_Notes = row.Cells["CSR_Notes"].Value.ToString() ?? "";

What am I missing?

1
  • You're still calling ToString() on null. You need to move the ?? inward to replace Value with something non-null. (row.cells[...].Value ?? "").ToString() Commented Sep 26, 2016 at 18:38

3 Answers 3

5

The issue is that Value is the potentially null property. By calling the ToString() method on it, you are creating the Null Reference Exception before the null coalescing operator has a chance to be used. You can fix this with the Null Propagating Operation from C# 6, like so:

deliveryEvent.CSR_Notes = row.Cells["CSR_Notes"].Value?.ToString() ?? "";

Notice the ? after Value. This is the Null Propagating operator. It will pass null through the rest of the expression if Value is null, which will then trigger the Null Coalescing Operator (??) properly.

The C# 5 solution would be to push the coalescing up a bit, as in haim770's answer.

deliveryEvent.CSR_Notes = (rows.Cells["CSR_Notes"].Value ?? "").ToString();
Sign up to request clarification or add additional context in comments.

5 Comments

That's only valid for C# 6.0
Yep, I was meaning to add in the fact that it would require C# 6 but missed the version # in the original post. Subsequent edits have corrected this. Thanks @haim770
I'm sorry Johathon, I had tried that approach as well without realizing that it was C# 6 only. I am still using vs2013. Looking into changing that soon.
I've added the C# 5 solution as well.
Yeah, that constraint was added after I initially answered. Both solutions are fine, and if there are solutions that are different based on tooling versions it's worth noting where those differences are. Null Propagation is a QoL feature and has a lot of potential alternative implementations.
1

You need to check that the row.Cells["CSR_Notes"] object is not null, but your last code snippet is checking the returned value of the entire expression row.Cells["CSR_Notes"].Value.ToString() and by the time it's evaluated it rightly throws because row.Cells["CSR_Notes"] returns null.

Try this instead:

deliveryEvent.CSR_Notes = (row.Cells["CSR_Notes"].Value ?? "").ToString();

See MSDN

1 Comment

That seems so obvious now haim770....it won't let me accept the answer yet, but I will. This did it.
0

You are calling .ToString() on an object that is null. That's why you receive the error. Here's a simpler reproduction:

MyObject obj = null;
string result = obj.ToString();

You can avoid this by using the null conditional operator (sometimes known as "elvis operator")

deliveryEvent.CSR_Notes = row.Cells["CSR_Notes"].Value?.ToString() ?? "";

This will only call .ToString() on Value if Value is not null.

3 Comments

The elvis operator usually indicates ?:, which in C# is a ternary conditional operator. This operator is the null conditional operator or the null propagating operator. Does the term elvis operator still apply in this case?
@JonathonChase I think it's still called the "elvis operator". Here's an article where they do so.
I seem to reject that internally, but it's a colloquialism so it is what we decide it is. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.