You are running into this issue based on the size of your temp_hop_id_list
. At the point of performing the division on list size your list size appears to be <= 1
.
Here is some logic I have created to illustrate how you would recieve a List Index Out Of Bounds: -1
error.
List<Contact> emptyCons = new List<Contact>();
if(emptyCons.size() > 0) {
system.debug('Woohoo');
} else {
system.debug('Below is a List Index Out Of Bounds -1 error.');
system.debug((cons.size()/2)-1);
}
Changes needed to your logic:
if(pnlNewYearsd.size() > 0) {
startYear = pnlNewYearsd.get(0);
} else {
if (temp_hop_id_list.size() >= 2) {
startYear = startYear == '' ? temp_hop_id_list[(temp_hop_id_list.size()/2)-1].Year__c : temp_hop_id_list[temp_hop_id_list.size()-1].Year__c;
} else {
// Exception Handling
}
}
Here you are making sure your list size is greater or equal than 2 to allow the division only ever end with a result of 0 or more.
I have also cleaned up all the if/else statements and used a ternary operator.
temp_hop_id_list
list seems to have a size of 0. Add a condition to manage this case. – SF_user Nov 29 '16 at 12:49