Skip to main content
Hi, I'm trying to have all pictures and pdfs in the attachment of a record displayed in a Visualforce page when a user klick on a button"View all files". I keep getting the Error:

"System.ListException: List index out of bounds: 0

Class.displayImageExtension.getFileId: line 15, column 1"

I'm an administrator and just beginning to code. Please help. Thank you.

here is my Code:

Controller:

Public Class displayImageExtension {

    String recId;

    

    public displayImageExtension(ApexPages.StandardController controller) {

        recId = controller.getId();    

    }

    

    public List<String> getFileId() {

        String[] fileId = new List<String>();

        List<Attachment> attachedFiles = [select Id from Attachment where parentId =:recId order By LastModifiedDate DESC ];

        if( attachedFiles != null && attachedFiles.size() > 0 ) {

        for (Integer i=0 ; i < attachedFiles.size(); i++) {   

            

            fileId[i] = attachedFiles[i].Id;

            }      

        }

        return fileId;

            

    }

}

Visualforce page:

<apex:page standardController="Account" extensions="displayImageExtension">

<apex:form >

<apex:image url="/servlet/servlet.FileDownload?file={!FileId}"/>

</apex:form>

</apex:page>
3 answers
  1. May 9, 2016, 6:33 PM
    Hi Suchawalee,

    What you are doing is trying to access 0 index of an empty List which is giving you IndexOutOfBoundException.

    Change the line,

    fileId[i] = attachedFiles[i].Id;

    with

    fieldId.add(attachedFiles[i].Id);

    Example :

     

    List<String> li = new List<String>();

    System.debug(li.isEmpty()); // true

    Also on page you must iterate the list,

     

    <apex:page standardController="Account" extensions="displayImageExtension">

    <apex:form >

    <apex:repeat value="{!FileId}" var="v">

    <apex:image url="/servlet/servlet.FileDownload?file={!v}"/>

    </apex:repeat>

    </apex:form>

    </apex:page>

    Hope it helps!

     
0/9000