+ Start a Discussion
Monishan MMonishan M 

List index out of bounds: 1 error

Hi All,

I am receiving the below error when running my Apex class

System.ListException: List index out of bounds: 1
Class.SubmitMedicalInquiryBatchMVN.execute: line 419, column 1

Below is the code piece where error is occuring 
      // Send Request
           
            //Boolean response = mirSubmission.fetchMISubmitResponse();
            String response = mirSubmission.fetchMISubmitResponse();
        
            val =  response.split(';');
          //  system.debug('value kk : ' +val[1]);
    //miverr =  new Map<Medical_Inquiry_vod__c , String >();
            if (val[0] == 'true') {
                system.debug('inside true kk');
                if (submittedInquiries == null) {
                    submittedInquiries = new List <MedicalInquirySubmissionMVN>();


The  fetchMISubmitResponse() method called from another Apex class:
The code piece is as below

 Method to parse the response and set the objects appropriately
*/
    public String fetchMISubmitResponse(){
        //String sReq = getMISubmitRequest();
        String sReq = getMISubmitRequestv2();
   
        
      String sResponse = SOAPServiceUtilityMVN.getSOAPResponse(EndPoint, sReq);
       
        integer count = sResponse.countMatches('<urn:description>');
        
        
        String[] value = sResponse.split('<urn:description>');
        String resdec  = '' ;
        for(integer i = 0 ; i < value.size() ; i++)
        {   
            System.debug('++++Value[i]'+Value[i]);
            if((value[i]).contains('</urn:description>'))
            {
                if(resdec !='') {
                    resdec +=','+ value[i].substringbefore('</urn:description>');
                    System.debug('value of str inside : ' +resdec); 
                }
                else {
                    resdec = value[i].substringbefore('</urn:description>');
                    System.debug('value of str inside : ' +resdec); 
                }
                
                
            }
        }
        System.debug('value of str out : ' +resdec);
        //String sResponse = '<urn:description> Origin country Code is Empty</urn:description>' ;
        //String resdec = sResponse.substring(sResponse.indexOf('<urn:description>') +17 , sResponse.indexOf('</urn:description>'));
        
        system.debug ('Response value from mule service kk : ' + resdec);
        
        
        // sResponse will SOMETIMES reavel the SOA Service password. If it does, destroy it for the debug.
        if (sResponse.contains(credentials.Password_MVN__c)) {
            
        } else {
            
        }
        
        DOM.Document doc = new DOM.Document();  
        try {
            doc.load(sResponse);
            DOM.XMLNode root = doc.getRootElement();
            DOM.XmlNode xBody = SOAPServiceUtilityMVN.getFirstChildElementByName(root, 'Body');
            DOM.XmlNode xFault = SOAPServiceUtilityMVN.getFirstChildElementByName(xBody, 'Fault');
            system.debug('value if xFault kkf : ' +xFault);
            DOM.XmlNode xException = null;
            for(DOM.XmlNode xNode:SOAPServiceUtilityMVN.getFirstChildrenOfLevel(xBody, 1)){
                if(xNode.getName() == 'exceptions'){
                    xException = xNode;
                    system.debug('value of Exception kke :' +xException);
                }
            }
            // Something failed in the SOA layer
            if(xFault != null || xException != null){
                return 'false' + ';' + resdec ;
            }
            else{
                String status = '0';
                InteractionStatus = new MedicalInquiryMVN.InteractionStatus();
                // Parse the response on submitting the AE to Jasper
                for(DOM.XmlNode xNode:SOAPServiceUtilityMVN.getFirstChildrenOfLevel(xBody, 2)){
                    if(xNode.getName() == 'statusMessage'){
                        InteractionStatus.StatusMessage = xNode.getText();
                    }
                    else if(xNode.getName() == 'submissionStatus'){
                        InteractionStatus.SubmissonStatus = xNode.getText();
                    }
                }
                
                if(InteractionStatus.SubmissonStatus == 'FAILED')
                {
                    system.debug('value mvnclass 298: ' +resdec);
                    return 'false' + ';' + resdec ;
                }
                else{
                    return 'true' + ';' + resdec ;
                }
                
            }
            system.debug('value mvnclass 306 : ' +resdec);
            return 'false' + ';' + resdec ;
        } catch (System.XMLException e) {  // invalid XML
            System.Debug('exception message : line 309 :' +e.getMessage());
            return 'false' + ';' + e.getMessage() ;
        }       
        return 'false' + ';' + resdec ;
    }   
}
                }
                submittedInquiries.add(mirSubmission);
            } else {
                system.debug('inside false');
                if (failedMIRSubmissions == null) {
                    failedMIRSubmissions = new List <Medical_Inquiry_vod__c>();
                }
                failedMIRSubmissions.add(thisMI);
                miverr.put(thisMI,val[1]);--------> error is occuring in this line
                system.debug('miverr value : ' +miverr);
            }
        }

Can you please help me in avoiding this error?

Thanks
AbhishekAbhishek (Salesforce Developers) 
If you see this error System.ListException: List index out of bounds 0 or 1or 2 .. error?


Let us understand by example.

List<Contact> cNames= new List<Contact>();
//get the Opportunity record details and its associated Account Details
ops=[SELECT Id,OwnerId,Name,Opportunity.AccountId,Account.Name
FROM Opportunity
WHERE Id IN:Trigger.New];

for(Opportunity o:ops){
//Create contact
Contact c= new Contact();
c.FirstName=”+ops[0].Account.Name;
c.LastName=”+ops[0].Account.Name;
c.Description=’Created by’+ops[0].Account.Name ;
cNames.add(c);
}


Imagine Trigger. New has only 1 record and so ops list has only 1 record.

When creating a contact, if I try to access c.FirstName=”+ops[1].Account.Name, I am trying to access an index that’s not on the List.

In such cases, List index out of bounds exception occurs.


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.