You need to sign in to do that
Don't have an account?

ListIndexOutOfBounds: 0 - but why?
Hello,
i have written the following code in apex:
public String getDateDiffs(){ List<Rental__c> tmpRents = new List<Rental__c>(); tmpRents.addAll(getVerleih()); List<String> strRents = new String[tmpRents.size()]; System.debug(tmpRents.size()); List<List<String>> listList; for(Integer i = 0; i < tmpRents.size();i++){ strRents.clear(); Date start = tmpRents.get(i).Start__c; Date ende = tmpRents.get(i).End__c; Integer toFirst = month.getfirstDate().daysBetween(start); Integer length = start.daysBetween(ende); System.debug(tmpRents.size()); System.debug(start); System.debug(ende); System.debug(toFirst); System.debug(length); strRents.add(toFirst,'|____'); System.debug(strRents); for(Integer k = toFirst+1;k < length;k++){ strRents.add(k,'_____'); } strRents.add(toFirst+length,'____|'); listList.add(i,strRents); } return strRents.get(0); }
It adds a string to a list on a certain position. This position is determined by the difference between a start date from the database and the beginning of the currently selected month.
When calling the method on a visualforce page, i get the message: List Index Out of Bounds: 0
I added every variable in a System.debug() to determine where the error comes from but i am not getting any wiser from the debug log:
16:12:07.094|METHOD_ENTRY|[326]|LIST.size() 16:12:07.094|METHOD_EXIT|[326]|LIST.size() 16:12:07.094|USER_DEBUG|[326]|DEBUG|27 16:12:07.094|METHOD_EXIT|[326]|System.debug(ANY) 16:12:07.095|METHOD_ENTRY|[327]|System.debug(ANY) 16:12:07.095|USER_DEBUG|[327]|DEBUG|2010-11-01 00:00:00 16:12:07.095|METHOD_EXIT|[327]|System.debug(ANY) 16:12:07.095|METHOD_ENTRY|[328]|System.debug(ANY) 16:12:07.095|USER_DEBUG|[328]|DEBUG|2010-11-05 00:00:00 16:12:07.095|METHOD_EXIT|[328]|System.debug(ANY) 16:12:07.095|METHOD_ENTRY|[329]|System.debug(ANY) 16:12:07.095|USER_DEBUG|[329]|DEBUG|0 16:12:07.095|METHOD_EXIT|[329]|System.debug(ANY) 16:12:07.095|METHOD_ENTRY|[330]|System.debug(ANY) 16:12:07.095|USER_DEBUG|[330]|DEBUG|4 16:12:07.095|METHOD_EXIT|[330]|System.debug(ANY) 16:12:07.095|METHOD_ENTRY|[331]|LIST.add(Integer, ANY) 16:12:07.095|EXCEPTION_THROWN|[331]|System.ListException: List index out of bounds: 0 16:12:07.095|METHOD_EXIT|[331]|LIST.add(Integer, ANY) 16:12:07.095|CODE_UNIT_FINISHED|calCon invoke(getdatediffs) 16:12:07.095|CODE_UNIT_FINISHED|calCon get(datediffs)
Judging from the debug log output, the position where the '|____' is inserted would be 0.
It seems that i am doing some minor mistake that i do not see it right now.
I defined the size of the list and judging from the debug output, it should be 27 in this case.
In the documentation for the list methods, it is described as follows:
add Integer i Any type e Void Inserts an element e into the list at index position i. In the following example, a list with six elements is created, and integers are added to the first and second index positions. List<Integer> myList = new Integer[6]; myList.add(0, 47); myList.add(1, 52); system.assertEquals(myList.get(1), 52);
It says for the add() method that you can have the Integer as a pointer and add the desired value.
Maybe i am overlooking something?
I would appreciate any help.
Regards,
hoomel
Hey hoomel
Your error is calling the following method strRents.clear(); which is removing all the elements of the list and setting its length to 0.
I spent a good 10 minutes staring intently at that before it hit me!
Removing that should do the trick :smileywink:
Paul
All Answers
Hey hoomel
Your error is calling the following method strRents.clear(); which is removing all the elements of the list and setting its length to 0.
I spent a good 10 minutes staring intently at that before it hit me!
Removing that should do the trick :smileywink:
Paul
Now that you say it, it perfectly makes sense :smileyvery-happy:
I guess i was just too focused to see a little thing like that.
Thank you very much for the help!!
You are welcome :-) Glad to help
Paul