Share via

SearchBox crashing

Anonymous
2022-07-22T18:42:02.09+00:00

Hi,

I have ListView in my Xamarin Forms app with SearchBox.

My ListView Bidning is like this:

SchedulesFlights.Root data = JsonSerializer.Deserialize<SchedulesFlights.Root>(result);  

and I am trying to use the SearchBox using below code:

private void OnFilterTextChanged(object sender, Xamarin.Forms.TextChangedEventArgs e)  
{  
    searchBar = (sender as SearchBar);  
  
    if (ListViewSchedulesArrival.DataSource != null && ListViewSchedulesArrival.IsVisible == true)  
    {  
        ListViewSchedulesArrival.DataSource.Filter = FilterService;  
        ListViewSchedulesArrival.DataSource.RefreshFilter();  
    }  
    else if (ListViewSchedulesDeparture.DataSource != null && ListViewSchedulesDeparture.IsVisible == true)  
    {  
        ListViewSchedulesDeparture.DataSource.Filter = FilterService;  
        ListViewSchedulesDeparture.DataSource.RefreshFilter();  
    }  
}  
  
private bool FilterService(object obj)  
{  
    if (searchBar == null || searchBar.Text == null)  
    {  
        return true;  
    }  
  
    var list = obj as SchedulesFlights.Response;  
  
    if (list.flight_iata.ToLower().Contains(searchBar.Text.ToLower()) || list.status.ToLower().Contains(searchBar.Text.ToLower()))  
    {  
        return true;  
    }  
    else  
    {  
        return false;  
    }  
}  

but every time the user types anything to search then the app will crash with the following error:

System.NullReferenceException: Object reference not set to an instance of an object
at Skyye.Schedules.FilterService (System.Object obj) [0x0002a] in /Users/jassim/Projects/Skyye/Skyye/Skyye/Schedules.xaml.cs:217
at Syncfusion.DataSource.DataSource.FilterRecord (System.Object record) [0x0000e] in <c4dd369a0b6a40d7857f6180c564514f>:0
at Syncfusion.DataSource.DataSource.InitializeItems () [0x00025] in <c4dd369a0b6a40d7857f6180c564514f>:0
at Syncfusion.DataSource.DataSource.Refresh () [0x00109] in <c4dd369a0b6a40d7857f6180c564514f>:0
at Syncfusion.DataSource.DataSource.RefreshFilter () [0x0009b] in <c4dd369a0b6a40d7857f6180c564514f>:0
at Skyye.Schedules.OnFilterTextChanged (System.Object sender, Xamarin.Forms.TextChangedEventArgs e) [0x0004a] in /Users/jassim/Projects/Skyye/Skyye/Skyye/Schedules.xaml.cs:199
at Xamarin.Forms.InputView.OnTextChanged (System.String oldValue, System.String newValue) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\InputView.cs:98
at Xamarin.Forms.InputView+<>c.<.cctor>b__49_0 (Xamarin.Forms.BindableObject bindable, System.Object oldValue, System.Object newValue) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\InputView.cs:9
at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00120] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:512
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:446
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:383
at Xamarin.Forms.Element.SetValueFromRenderer (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:248
at Xamarin.Forms.Element.Xamarin.Forms.IElementController.SetValueFromRenderer (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:244
at Xamarin.Forms.Platform.iOS.SearchBarRenderer.UpdateOnTextChanged () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\SearchBarRenderer.cs:352
at Xamarin.Forms.Platform.iOS.SearchBarRenderer.OnTextChanged (System.Object sender, UIKit.UISearchBarTextChangedEventArgs a) [0x00013] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\SearchBarRenderer.cs:227
at UIKit.UISearchBar+_UISearchBarDelegate.TextChanged (UIKit.UISearchBar searchBar, System.String searchText) [0x00011] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/build/ios/native/UIKit/UISearchBar.g.cs:1387
at at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, System.Type principalClass, System.Type delegateClass) [0x0003b] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:85
at Skyye.iOS.Application.Main (System.String[] args) [0x00001] in /Users/jassim/Projects/Skyye/Skyye/Skyye.iOS/Main.cs:17

Developer technologies | .NET | Xamarin
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Viorel 127K Reputation points
    2022-07-23T04:36:57.067+00:00

    Probably obj is not a SchedulesFlights.Response. Clarify the real type of obj. Maybe you can write:

    var list = (SchedulesFlights.Root)obj;

    and adjust the code.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.