-2

I am getting an error with this code:

if (!App.selectedPhrases.Any())

Here's the error message:

Xamarin Exception Stack: System.ArgumentNullException: Value cannot be null. Parameter name: source at System.Linq.Enumerable.Any[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x0000d] in :0 at Japanese.DeckTabViewModel.ShowFirstMessageAsync () [0x00029] in <75f30b75b8d1435e80e8b7703a671cac>:0 at Japanese.DeckTabViewModel.OnAppearing () [0x0006c] in <75f30b75b8d1435e80e8b7703a671cac>:0 at Japanese.DeckTabPage.OnAppearing () [0x0006d] in <75f30b75b8d1435e80e8b7703a671cac>:0 at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__7_0 (System.Object state) [0x00000] in <3ad100fe60d44e1c8a81197ba1997e7e>:0 at Android.App.SyncContext+<>c__DisplayClass2_0.b__0 () [0x00000] in :0 at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in :0 at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in :0 at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.34(intptr,intptr)

Does anyone have any ideas on what might be happening, how I could find the exact line number in the exception and then also how I could resolve this.

In particular I am confused as to why the stacktrace doesn't show any line number. Does anyone know why this is?

One more question. How can I avoid the use of a "!" here as I find this very confusing.

Alan2
  • 23,493
  • 79
  • 256
  • 450
  • Most likely `App.selectedPhrases` is null – Nkosi Feb 03 '20 at 00:59
  • for the first question, use `App.selectedPhrases?.Any()` for the second question, you can do `if(App.selectedPhrases.Any() == true)` or `if(App.selectedPhrases.Any() != true)` ..etc. – iSR5 Feb 03 '20 at 01:00
  • 1
    While an exact line number was not given, the error hints at where the issue originates `Japanese.DeckTabViewModel.ShowFirstMessageAsync () => Japanese.DeckTabViewModel.OnAppearing () => Japanese.DeckTabPage.OnAppearing()` Do you have an `async void` somewhere there? It could be executing on a separate thread before the member has been set. This leads me to believe this might be an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Nkosi Feb 03 '20 at 01:04

1 Answers1

2

You will have to check for null on selectedPhrases, This can be done easily with the

Null-conditional operator ?.

Available in C# 6 and later, a null-conditional operator applies a member access, ?., or element access, ?[], operation to its operand only if that operand evaluates to non-null; otherwise, it returns null.

However because of lifting, you will have to explicitly specify false on the if condition

if(App.selectedPhrases?.Any() == false)

or if you want to check check for null explicitly as part of the condition, you can use short circuiting (Conditional logical OR operator)

The conditional logical OR operator ||, also known as the "short-circuiting" logical OR operator, computes the logical OR of its operands. The result of x || y is true if either x or y evaluates to true. Otherwise, the result is false. If x evaluates to true, y is not evaluated.

if(App.selectedPhrases == null || !App.selectedPhrases.Any())
TheGeneral
  • 79,002
  • 9
  • 103
  • 141