Very simple question, the following code throws a "Nulllable object must have a value" exception. But I thought that the ?? was going to handle that?
string qty = stop.pallets_qty_total.Value.ToString() ?? "0";
Very simple question, the following code throws a "Nulllable object must have a value" exception. But I thought that the ?? was going to handle that?
string qty = stop.pallets_qty_total.Value.ToString() ?? "0";
You are checking for null
on the result that is being returned after calling the ToString()
method on the Value
property of stop.pallets_qty_total
while there is chance that stop.pallets_qty_total
is null
and it is also possible that the stop
itself is null
which will also cause your code to go in failure.
You need try something like following using Null-Propogation operator:
string qty = (stop?.pallets_qty_total?.ToString()) ?? "0";
Although the comments already gives the correct answer, here a more detailed explanation:
Your are right that the ??
operator will take the left operand it its value is not null. Otherwise it will take the right operand. It is essential a short version for:
var qty = input == null ? "0" : input;
However the exception is not thrown by the assignment. The exception is thrown by the call to ToString()
.
If your input is null
your statement equals the following:
var qty = ((object)null).ToString();
Which obviously throws a NullReferenceException
.
What you are looking for is the ?.
operator. Which says "call the function if the object is not null". This expands into the following:
input != null ? input.ToString() : null
// is equal to
input?.ToString()
So the solution for the assignment should be a combination of the two operators:
var qty = input?.ToString() ?? "0";
You are trying to access [Value][1]
property of Nullable<T>
when it does not have the value assigned. As specification says, it will throw and exception:
If a value of type T has not been assigned to the Nullable object, you can compare it to null and retrieve its HasValue property, but you cannot access its Value property or call its other members.
You have two options here - use null-conditional operator to avoid accessing object properties if object is null:
string qty = stop.pallets_qty_total?.Value.ToString() ?? "0";
Or you can keep in mind that default value for numeric type is zero. And get default value if nullable does not have value assigned:
string qty = stop.pallets_qty_total.GetValueOrDefault().ToString();
That will be also tiny premature performance optimization.
Just as an aside to Ehsan Sajjad's answer, you actually don't need the second ?
here, assuming pallets_qty_total
is an integer. You can instead use Nullable.GetValueOrDefault(), like so:
string qty = stop?.pallets_qty_total.GetValueOrDefault().ToString();
If pallets_qty_total
has a value, it will return it. If not, it will return default(T)
, which, in the case of an int
, is 0
.
It will only check if the Value is null but if object before is null then it well throw exception.
string qty = stop?.pallets_qty_total?.Value.ToString() ?? "0";