简体   繁体   中英

Handling a null exception C#

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

If the problem is that rowHandles can be null then just add an explicit check for that which prevents you from executing the for statement.

if ( rowHandles != null ) { 
  for ( int i = 0; i < rowHandles.Length; i++ ) {
    ...
  }
}

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.

The problem is the delete button can be clicked when there are no rows left, so rowHandles.Length is equal to null.

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.

It's not rowHandles.Length which is null, it's rowHandles itself.
A common solution would be:

if (rowHandles != null)  
//Your loop here

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:

if (rowHandles != null)
{
    for (int i = 0; i < rowHandles.Length; i++)
    {
          // Code
    }
}

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.

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.

if(rowHandles != null)
{
    //for loop
}

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 is null .

If you still choose to handle it in the code body you're referencing, any of the suggested solutions will work.

Change to a foreach loop:

foreach (var rowHandle in rowHandles) 
{ 
    // use rowHandle instead of rowHandles[i]
} 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM