Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing Index Out of Range Error

Tags:

c#

I want to write a check for some conditions without having to use try/catch and I want to avoid the possibilities of getting Index Out of Range errors

if (array.Element[0].Object.Length > 0 || array.Element[1].Object.Length > 0) //making sure there's at least one Object array that has values
 {                
    if (array.Element[0].Object[0].Item.Length != 0 || array.Element[1].Object[0].Item.Length != 0) //this is where I check that at least one of the Items (strings) is not empty
       {
           // execute code here
       }
 }

So the problem I am facing is that in the second check I need to see whether I have one Item that is not empty. However, If I don't have Element[1], I get the Index Out of Range exception. The problem is that there could be 2 Elements and one(or both) of them may have empty Object arrays. The code will have to be executed only if one of thos Item strings is not empty.

Hopefully, I explained it well. How do I go about avoiding getting that exception under any condition?

like image 777
Victor Avatar asked Apr 11 '12 20:04

Victor


People also ask

What does the error list index out of range mean?

Generally, list index out of range means means that you are providing an index for which a list element does not exist.

What is index out of range error in Python?

To solve the “indexerror: list index out of range” error, you should make sure that you're not trying to access a non-existent item in a list. If you are using a loop to access an item, make sure that loop accounts for the fact that lists are indexed from zero.

What causes index error?

IndexError is an exception in python that occurs when we try to access an element from a list or tuple from an index that is not present in the list. For example, we have a list of 10 elements, the index is in the range 0 to 9.

How do you fix a string index out of range in Python?

The “TypeError: string index out of range” error is raised when you try to access an item at an index position that does not exist. You solve this error by making sure that your code treats strings as if they are indexed from the position 0.


1 Answers

Alright, you need some better null checking and some more cautious code here.

if (array.Element[0].Object.Length > 0 || array.Element[1].Object.Length > 0) //making sure there's at least one Object array that has values
{                
   if (array.Element[0].Object[0].Item.Length != 0 || array.Element[1].Object[0].Item.Length != 0) //this is where I check that at least one of the Items (strings) is not empty
   {
       // execute code here
   }
}

is just unacceptable.

First, let's null check

if (array != null)
{
    if (array.Element != null)

for simplicity, you could use &&

if (array != null && array.Element != null)

then, inside that if, we use a for loop (since you're stuck on arrays) and null check it

for (int i = 0; i < array.Element; ++i)
{
    if (array.Element[i] != null && array.Element[i].Object != null)
    {

then, since you have nested arrays, we loop again. This is called a nested loop, and it's generally bad practice, I'll show you why it works in a second.

for (int o = 0; o < array.Element[i].Object.length; ++o)
{
    if (array.Element[i].Object[o] != null && !string.IsNullOrEmpty(array.Element[i].Object[o].Item))
    {

Now, with all of that ugly nested loopyness, we've found out that your Item is not null. On top of that, you have access to ALL of the potential values here, and can group them as you like. Here's how I would put the whole thing together for simplification.

List<string> arrayValues = new List<string>();
if (array != null && array.Element != null)
{
    for (int i = 0; i < array.Element.length; ++i)
    {
        //bool found = false;
        if (array.Element[i] != null && array.Element[i].Object != null)
        {
            for (int o = 0; o < array.Element[i].Object.length; ++o)
            {
                if (array.Element[i].Object[o] != null && !string.IsNullOrEmpty(array.Element[i].Object[o].Item))
                {
                    arrayValues.Add(array.Element[i].Object[o].Item);
                    //if you want to drop out here, you put a boolean in the bottom loop and break and then break out of the bottom loop if true
                    //found = true;
                     //break;
                }
            }
        }
        //if (found)
        //  break;
    }
}
if (arrayValues.Count > 0)
{
    //do stuff with arrayValues
}
like image 107
deltree Avatar answered Oct 14 '22 02:10

deltree