6

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

1
  • You forgot to mention Cornell in your post. Commented Jan 18, 2011 at 20:39

8 Answers 8

10

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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, don't feel stupid. Mistakes are the best way to learn and we've all made plenty of them.
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?
6

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.

2 Comments

Apart from it being a bad practice, does letting a null reference exception happen (and then catching it) cause any problems with the programs state? For instance I know that some exceptions can corrupt program state, but does the .NET runtime make sure that null-reference doesn't do this?
@Matt: Reading, writing or invoking a null reference does not change state; the exception always happens before anything bad can be written.
3

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.

Comments

2

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.

Comments

2

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

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

Comments

1

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
}

Comments

1

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.