处理空异常 C#
好的,新编码员在这里寻找对这个问题的一些见解。所以我有一个像这样开始的 for 循环:
for (int i = 0; i < rowHandles.Length; i++)
{
.........code....
}
rowHandles 是一个包含数据行的 int 数组。该 for 循环的代码会在单击删除按钮时删除数据行,具体来说,它是一个网格工具条删除按钮,并且位于删除按钮单击事件处理程序内。问题是当没有剩余行时可以单击删除按钮,因此 rowHandles.Length 等于 null。我该如何防止这个停止程序?我可以在 for 循环内部、for 循环声明中或 for 循环外部添加一些内容来纠正这个问题吗?也许尝试一下?如何围绕这个特定问题/循环构建它?
感谢您的帮助 - 新手编码器
Ok, new coder here looking for a little insight into this problem. So I have a for loop like that starts like this:
for (int i = 0; i < rowHandles.Length; i++)
{
.........code....
}
rowHandles is an int array that contains rows of data. This for loop has code that deletes the rows of data when a delete button is clicked, to be specific it is a grid tool strip Delete button and this is inside the delete button click event handler. The problem is the delete button can be clicked when there are no rows left, so rowHandles.Length is equal to null. How would I prevent this from stopping the program? Is there something I could add inside the for loop, in the for loop declaration, or outside the for loop to correct this? Maybe a try catch? How would this be structured around this specific problem/loop?
Thanks for all your help - Newbie Coder
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果问题是
rowHandles
可能为null
,那么只需添加一个显式检查即可阻止您执行for
语句。另一种选择是,如果没有要删除的行,则完全禁用删除按钮。该操作无效,因此从一开始就阻止它。
If the problem is that
rowHandles
can benull
then just add an explicit check for that which prevents you from executing thefor
statement.Another option would be to disable the delete button altogether if there are no rows to be deleted. The operation is invalid so prevent it from the start.
这里的一个重要原则是永远不要处理本来可以阻止的异常。你永远不应该处理空引用异常;空引用异常表示程序中应该修复的错误,而不是应该捕获和忽略的预期发生的情况。要么编写代码确保该值不为空,要么检测它为空并且不取消引用它。
An important principle here is never handle an exception that you could have prevented in the first place. You should never ever ever handle a null reference exception; a null reference exception indicates a bug in your program that should be fixed, not an expected occurrance that should be trapped and ignored. Either write code that ensures that the value is not null, or detect that it is null and do not dereference it.
这是错误的。当有0个元素时,Length设置为0。null可能是rowHandles。在进入循环之前,您需要处理该情况。
This is wrong. When there are 0 elements, Length is set to 0. What is null is probably rowHandles. You need to handle that condition before you get into your loop.
如果没有剩余行,则 rowHandles.Length 将为零而不是 null。如果您在循环后摆脱 rowHandles,那么您可以这样做:
不需要异常处理。另一方面,如果在循环运行时允许 rowHandles 被其他内容更改,那就是另一回事了。
If there are no rows left, rowHandles.Length will be zero not null. If you're getting rid of rowHandles after the loop, then you can just do:
No need for exception handling. On the other hand if you're allowing rowHandles to be changed by something else while that loop is running, that's another story.
不是
rowHandles.Length
为 null,而是rowHandles
本身。一个常见的解决方案是:
It's not
rowHandles.Length
which is null, it'srowHandles
itself.A common solution would be:
看起来 Length 并不是空值。相反, rowHandles 为 null,并且您在尝试访问 Length 属性时收到 null 异常。
It looks like Length is not the thin that is null. Rather it is rowHandles that is null and you are getting the null exception when trying to access the Length property.
我建议您尽可能坚持单一职责原则,因为您让代码执行其预期的操作,并在其他地方处理错误。
对我来说,在其他地方使用
rowHandles
是有意义的,因此您应该集中检查它是否为null
的过程。如果您仍然选择在引用的代码主体中处理它,则任何建议的解决方案都可以工作。
I would suggest you try as much as possible to stick to the Single Responsibility Principle, in that you let the code do what it is intended to do, and handle the errors elsewhere.
It makes sense to me that
rowHandles
is used elsewhere, so you should centralize the process of checking whether or not it isnull
.If you still choose to handle it in the code body you're referencing, any of the suggested solutions will work.
更改为
foreach
循环:这样,如果整个 rowHandles 对象不为 null(快速 null 检查可以验证这一点),您将迭代所有项目,如果没有项目,您将不会迭代根本不。
Change to a
foreach
loop:This way, provided the entire rowHandles object is not null (a quick null check can verify this) you will iterate over all items, and if there are no items, you wont iterate at all.