Nard Dog
Nard Dog

Reputation: 916

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

Upvotes: 6

Views: 3353

Answers (8)

Eric Lippert
Eric Lippert

Reputation: 660573

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.

Upvotes: 6

bradenb
bradenb

Reputation: 793

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.

Upvotes: 1

Oren A
Oren A

Reputation: 5900

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

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

Upvotes: 2

Seattle Leonard
Seattle Leonard

Reputation: 6776

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
}

Upvotes: 1

Gabriel Magana
Gabriel Magana

Reputation: 4536

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.

Upvotes: 3

Chris S
Chris S

Reputation: 65476

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.

Upvotes: 2

Nate
Nate

Reputation: 30666

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.

Upvotes: 0

JaredPar
JaredPar

Reputation: 755587

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.

Upvotes: 10

Related Questions