0

I have a list with 16967 items and when querying the list and no results are found instead of count = 0 or zero results it throws, "System.NullReferenceException: Object reference not set to an instance of an object" is throwing.

var result = englishWords.Where(c => c.engWord.StartsWith(searchWord.ToLower()));

when the result of the querying is no results, the variable "result" is throwing null reference exception instead of zero results or no result. Any help would be appreciated?

[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object. 12-28 13:32:08.284 E/mono-rt ( 8929): at TigrinyaDictionary.Views.ItemsPage+<>c__DisplayClass20_0.<MainSearchBar_TextChanged>b__0 (TigrinyaDictionary.Models.EngWord c) [0x00000] in C:\Users\Default\Desktop\Projects\Dictionary\MobileApp\TigrinyaDictionary\TigrinyaDictionary\Views\ItemsPage.xaml.cs:202 12-28 13:32:08.284 E/mono-rt ( 8929): at System.Linq.Enumerable+WhereSelectListIterator2[TSource,TResult].MoveNext () [0x00037] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Linq/src/System/Linq/Where.cs:574 12-28 13:32:08.284 E/mono-rt ( 8929): at System.Linq.Enumerable+EnumerablePartition1[TSource].ToList () [0x00012] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Linq/src/System/Linq/Partition.cs:629 12-28 13:32:08.284 E/mono-rt ( 8929): at System.Linq.Enumerable.ToList[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x0000e] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Linq/src/System/Linq/ToCollection.cs:30 12-28 13:32:08.284 E/mono-rt ( 8929): at TigrinyaDictionary.Views.ItemsPage.MainSearchBar_TextChanged (System.Object sender, Xamarin.Forms.TextChangedEventArgs e) [0x00220] in C:\Users\Default\Desktop\Projects\Dictionary\MobileApp\TigrinyaDictionary\TigrinyaDictionary\Views\ItemsPage.xaml.cs:205

enter image description here

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
kiflay
  • 699
  • 2
  • 7
  • 14
  • 1
    One of the entries in `englishWords` is `null` or has a `engWord` property that is `null`. – mjwills Dec 28 '20 at 13:23
  • 1
    Don't use null like mjwills suggests. A When you not return a null. Instead use result.Count() > 0; – jdweng Dec 28 '20 at 13:25
  • The `IEnumerable` returned by `Where` has no `Count` property. It has a `Count()` *method*. What you posted shouldn't even compile. The screenshot doesn't show any NullReferenceException, it shows that if you try to retrieve an IEnumerator instance from the query result while the debugger has stopped the application, you'd get an exception. `result` is *always* non-null – Panagiotis Kanavos Dec 28 '20 at 13:29
  • Sorry I have two variables: result and results: Here is how the code looks like var result = englishWords?.Where(c => c.engWord.StartsWith(searchWord.ToLower())); if (result != null) { results = result.Select(c => c.engWord).Take(5).ToList(); } – kiflay Dec 28 '20 at 13:38
  • 1
    Get rid of the `null` check - it does nothing useful. – mjwills Dec 28 '20 at 13:40
  • @PanagiotisKanavos in the watch window you can see the "result" variable is throwing a null reference exception – kiflay Dec 28 '20 at 13:40
  • 1
    Now that we have the stack trace, it looks like the actual exception occured *inside* the `Where` call, most likely because one of the `engWord` values was a `NULL` to begin with. Either check for `null` in `Where` or use the null-safe operator, `?.`, eg `.Where(c => c.engWord?.StartsWith(searchWord.ToLower())`. Or perhaps `searchWord` is null? – Panagiotis Kanavos Dec 28 '20 at 13:40
  • 1
    @kiflay that image is irrelevant and the error doesn't even occur in there. The exception's stack trace occurs in a different place, inside the `Where` call – Panagiotis Kanavos Dec 28 '20 at 13:41
  • 1
    Does changing to `var result = englishWords.Where(c => c?.engWord?.StartsWith(searchWord?.ToLower() ?? string.Empty) ?? false);` work? – mjwills Dec 28 '20 at 13:41
  • 1
    BTW you don't need `ToLower()` either. You can use [String.StartsWith(string,StringComparison)](https://learn.microsoft.com/en-us/dotnet/api/system.string.startswith?view=net-5.0#System_String_StartsWith_System_String_System_StringComparison_) to perform a case-insensitive search – Panagiotis Kanavos Dec 28 '20 at 13:42

0 Answers0