tags:

views:

164

answers:

8

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

+9  A: 

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.

JaredPar
Thank you, I feel kind of stupid because I tried it the opposite way: for(...){if(rowhandles != null)} and that didn't work so I was scratching my head, looks like I had the right idea just wrong execution. I will get there! Thanks for your input!
Nard Dog
@Nard, don't feel stupid. Mistakes are the best way to learn and we've all made plenty of them.
JaredPar
@JaredPar I go back and forth on this; I see a lot of null checking code that hides bugs because developers aren't willing to find out why something is null. In this case, looks like null is not valid, so I'm not sure allowing a null is a good idea. I would prefer to disable the button and not leave the null check. Puts the burden on the caller, but you'll fail immediately if somebody calls it with rowHandles being null. Any thoughts?
Juan Mendes
+2  A: 

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

if (rowHandles != null)  
//Your loop here
Oren A
+3  A: 

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.

gmagana
A: 

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.

Nate Bross
+2  A: 

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.

Chris S
+1  A: 

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
}
Seattle Leonard
+1  A: 

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.

Brazzle
+5  A: 

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.

Eric Lippert