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
prazonprazon 

Visualforce Error Help for this Page Subscript is invalid because list is empty

Hi,

 

How to handle this error. Although I have put exception handler in the class this error is not getting trapped. If anybody knows kindly apprise me off

 

Regards

 

Prazon

bob_buzzardbob_buzzard

Can you post the code that is causing the problem?

prazonprazon
public with sharing class Account_Flag_Status_Controller {
    
    public Id AccountId{get;set;}
    public Id flagId{get;set;}
    public Account mainAccount;
    public List<Flag_Reporter__c> flagReport{get;set;}
    public List<Flag_Reporter__c> flagReported{get;set;}
    
    public Account_Flag_Status_Controller(ApexPages.StandardController controller) {
        mainAccount= (Account)controller.getRecord();
        AccountId = mainAccount.Id;
        try{
        flagReport = [Select Overall_Status__c, LeadQualityFlag__c, CreditCheckFlag__c,Venue__c, CompanyAgeFlag__c,Flag_for_Editorial__c, WebsiteFlag__c, Overall_Mystery_Shopping__c,Overall_PM_Assessment__c,Overall_Escalation__c, Flag_comments__c, Overall_comments__c  from Flag_Reporter__c Where Account_Flag__c=:AccountId LIMIT 1];
        flagId =[Select Id from Flag_Reporter__c where Account_Flag__c=:AccountId LIMIT 1].Id;
        }catch (Exception dml) {
            pageError(dml);                 
        }
        flagReported = new List<Flag_Reporter__c>();
    }
    
     public PageReference pageError(Exception dml){
         //ApexPages.addMessages(dml);    
         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Account Quality NOT EXISTING Please edit and save the account'));
         return null;
     }
    
   
bvramkumarbvramkumar

Did you bind flagReport or flagReported to the VF page with a merge field? a getter might be executing for these lists on load. try returning a list with one item in the getter for flagReport to confirm the issue first. You might be instantiating in your constructor but it is a parameterized constructor. Not sure but chances might be that it is executing default constructor and not finding your list instantiated. Just a thought.... So try some thing like this to confirm the issue...

 

 public List<Flag_Reporter__c> flagReport{get {return new List<Flag_Reporter__c>(){ new Flag_Reporter__c(Name = 'test flag report 123')}; set;} 
bob_buzzardbob_buzzard

In both of the queries in the constructor you are assuming that there will be at least one record that satisfies the criterial - is this guaranteed to be the case?

prazonprazon
It looks like the error is coming due to non-availability of records and I am trying to catch that.
bob_buzzardbob_buzzard

From the sound of it you aren't able to catch it though.  If that is the case you'd do better to extract a list and check the size, something like:

 

List<Flag_Reporter__c> flagReports= [Select Overall_Status__c, LeadQualityFlag__c, CreditCheckFlag__c,Venue__c, CompanyAgeFlag__c,Flag_for_Editorial__c, WebsiteFlag__c, Overall_Mystery_Shopping__c,Overall_PM_Assessment__c,Overall_Escalation__c, Flag_comments__c, Overall_comments__c  from Flag_Reporter__c Where Account_Flag__c=:AccountId LIMIT 1];

if (flagReports.size()==1)
{
    flagReport=flagReports[0];
}
else
{
   // handle error
}