使用 linq 时出现 NullReferenceException
当我从界面中检查某些复选框时,以下代码行会抛出 NullReferenceException 错误,但当我检查所有复选框时,不会发生异常。每个选中的复选框都会导致创建一个对象,并且该对象存储在同一类的数组中。
someObjects.OrderByDescending(obj => obj.numericProperty);
数组 - someObjects - 包含一些我想要排序的选中复选框中选定的对象。数组 someObjects 也与对象具有相同的类型。请问我该如何解决这个问题?谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题不清楚,但听起来
someObjects
包含一些空引用。最好将它们过滤掉:
或者,避免将它们放入数组中。目前还不清楚数组是如何构建的,但是其中的空引用对您来说有用吗?
Your question is unclear, but it sounds like
someObjects
contains 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?