The following line of code throws NullReferenceException error When I check some checkboxes from the interface, but when I check all the checkboxes the exception won’t occur. Each checkbox checked will cause an object to be created, and the objects are stored in an array of the same class.
someObjects.OrderByDescending(obj => obj.numericProperty);
The array – someObjects – contains some selected objects from some of the checked checkboxes which I want to sort. The array someObjects is also of the same type with the objects. Please how do I fix this? Thanks.
Your question is unclear, but it sounds like
someObjectscontains some null references.It’s probably best to just filter them out:
Or alternatively, just avoid putting them in the array to start with. It’s not really clear how the array’s being built, but is it useful to you to have null references in there?