function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Out of bounds error

I have a controller for a page and I intermittantly get an out of bounds error on this line:

pdEvent = pdEventList[0]; 


Is there anything I am doing wrong? Since I get the out of bounds error I am assuming my query is sometimes producing 0 results which doesn't make sense since the Id will be in the URL.


Also, my VF page is not an embedded page in the UI, it overrides the whole page. So we use 

<apex:detail id="pdEventDetailsSection" subject="{!PD_Event.Id}" inlineEdit="false" 
reRender="pageBlockId" />


to show the regular page layout with our custom VF stuff up at the top. Maybe that has something to do with my controller not finding the Id


code snippet of my constructor for my apex controller

 public controller_PDEvent(ApexPages.StandardController stdController) {
        system.debug(LoggingLevel.INFO, ' --- begin - controller_PDEvent --- ');
        isSelectedActivePartnerPortalUsersRendered = false ;
        isSaveRendered = false ;
         Professional_Development_Event__c [] pdEventList = [ 
         	SELECT Id, Training_Location_Name__c, Training_Site_Room__c, Recipient_Group__c, Training_Location_Street__c, Training_Location_State__c ,  Training_Location_City__c , Training_Location_Zip__c , Training_Site__c 
         	FROM Professional_Development_Event__c 
         	WHERE Id = :stdController.getId() 
		pdEvent = pdEventList[0]; 



Is it best practice to do something like:


if (pdEventList.size() == 0)

Shailesh DeshpandeShailesh Deshpande

in your case you could check whether you are getting the id or not, and only then proceed agead. Yes if you are trying to assign a value from the list, it is always better to check whther there are any elements present in it or not. You may use the isEmpty() method of the list or the way you did in your second statement.

I am not sure if this would make any difference or not, but you could give it a try:

instead of using stdController.getId(), try using ApexPages.currentpage().getParameters().get('id'),

Thanks, I'll try that. It's a tough one to know if I fixed because it happens very rarely