 ShowAll Questionssorted byDate Posted CW5

# List index out of bounds: 1 - How to fix?

ERROR ON LINE -

`if(lstcoa3[i].Contact__c == c.id) `

I know what the error is, but am not sure how to fix it.

count is sometimes higher than the lists (lstcoa1, lstcoa2, lstcoa3, lstcoa4) because they are different sizes.lstcoa1.size() could be 10 and lstcoa2.size() could be 25.

I need it to to loop through and only calculate if "i" will return for lstcoa1[i] and not provide an error.

I've tried basic if statements that say if(lstcoa1.size() < i) but that didn't  seem to work.

Thoughts on how to get this error to go away?

```for(Integer i = 0; i < count; i++)
{

if(lstcoa1[i].Contact__c == c.id)
{
numSent = numSent + 1;
}

if(lstcoa2[i].Contact__c == c.id)
{
numOpened = numOpened + 1;
}

if(lstcoa3[i].Contact__c == c.id)
{
numClicked = numClicked + 1;
}

if(lstcoa4[i].Contact__c == c.id)
{
numBounced = numBounced + 1;
}
}
``` Best Answer chosen by Admin (Salesforce Developers)  Wizrad

I assume count is the size of the largest lstcoa list?

If so just set your if statements up like this:

` if(i < lstcoa1.size() && lstcoa1[i].Contact__c == c.id)`

Saying lstcoa1.size() < i is false logic.  Think about the result of i = 0 and lstcoa1 is 10.

### All Answers Wizrad

I assume count is the size of the largest lstcoa list?

If so just set your if statements up like this:

` if(i < lstcoa1.size() && lstcoa1[i].Contact__c == c.id)`

Saying lstcoa1.size() < i is false logic.  Think about the result of i = 0 and lstcoa1 is 10.

This was selected as the best answer CW5

Thanks Wizrad!!! :smileyhappy: