0

I get this error: System.ArgumentNullException: Value cannot be null. But that is the point right? if .FreeAmount is null it should check in the AmountList for a Selected amount. However it fails doing so. I can't really see what I have done wrong here.

var amount = string.IsNullOrEmpty(formModel.FreeAmount)
  ? formModel.AmountList
      .Where(x => x.Selected)
      .Select(a => a.Value)
      .SingleOrDefault()
  : formModel.FreeAmount;
10
  • 1
    Have you checked if formModel is null? Commented Aug 21, 2017 at 8:15
  • 1
    Is there a SelectedAmount in AmountList ? Commented Aug 21, 2017 at 8:19
  • Please include formModel's class as welll as AmountList's class. Commented Aug 21, 2017 at 8:24
  • 3
    @Kostis if fromModel was null then you would get NullReferenceException, not ArgumentNullException. Commented Aug 21, 2017 at 8:24
  • Just a suggestion: formModel.AmountList.Where(x => x.Selected).DefaultIfEmpty(0).Select(a => a.Value).SingleOrDefault() Commented Aug 21, 2017 at 8:33

2 Answers 2

2

You will only get a System.ArgumentNullException in the code you posted if both FreeAmount and AmountList are null. If there is some other default value you want to use in this case, you could do something like:

var amount = string.IsNullOrEmpty(formModel.FreeAmount)
    ? formModel.AmountList == null
        ? "Default value if FreeAmount and AmountList are null"
        : formModel.AmountList
            .Where(x => x.Selected)
            .Select(a => a.Value)
            .SingleOrDefault()
    : formModel.FreeAmount;

Otherwise, if you just want it to return null in this case (which I suppose is more likely, since that's currently a possibility with SingleOrDefault) then you can just use the null-conditional operator (?.):

var amount = string.IsNullOrEmpty(formModel.FreeAmount)
    ? formModel.AmountList? // <-- The '?' will return null if AmountList is null
        .Where(x => x.Selected)
        .Select(a => a.Value)
        .SingleOrDefault()
    : formModel.FreeAmount;
Sign up to request clarification or add additional context in comments.

Comments

-1

In the above statement,

Left had side of the ternary operator can result in NULL if formModel is NULL,

So, string.IsNullOrEmpty(formModel.FreeAmount) => will result in TRUE.

So, the ternary operator will evaluate left side (true) of the : symbol which is,

formModel.AmountList
      .Where(x => x.Selected)
      .Select(a => a.Value)
      .SingleOrDefault()

This will result in System.ArgumentNullException because formModel value is null.

So, in the case of formModel being NULL the command will fail with System.ArgumentNullException.

2 Comments

This is not correct. If formModel is null then formModel.FreeAmount will throw a NullReferenceException. It will never execute string.IsNullOrEmpty nor get to the ternary operator.
It could get into the ternary if formModel.FreeAmount was instead formModel?.FreeAmount - but even so the ternary would still throw a NullReferenceException.

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.