+ Start a Discussion
Abraham kumar 4Abraham kumar 4 

List index out of bounds: 0

Please help me resolve this error, occuring at the below code line. how to overcome this 
Line no:3 is where the error occurs.
if(EventId != Null){
            event = [Select Id,Name from Event__c where Id =: EventId LIMIT 1];
            eventName = event[0].Name;
        }

Many Thanksin advance

Abraham



 
Best Answer chosen by Abraham kumar 4
Amit Chaudhary 8Amit Chaudhary 8
Try below code:-
if(EventId != Null)
{
            event = [Select Id,Name from Event__c where Id =:EventId LIMIT 1];
            if(event.ize() > 0)
            {
               eventName = event[0].Name;
            }
}


 

All Answers

Shrikant BagalShrikant Bagal
Hello ,

Please try
 
if(EventId != Null){
            event = [Select Id,Name from Event__c where Id =: EventId LIMIT 1];
            if(!event.isEmpty()){
                eventName = event[0].Name;
            }            
        }

Your query will not return any result so you are getting this error.


If its helps, please mark as best answer so it will help to other who will serve same problem.
​Thanks! 
Amit Chaudhary 8Amit Chaudhary 8
Try below code:-
if(EventId != Null)
{
            event = [Select Id,Name from Event__c where Id =:EventId LIMIT 1];
            if(event.ize() > 0)
            {
               eventName = event[0].Name;
            }
}


 
This was selected as the best answer
Abraham kumar 4Abraham kumar 4
Thanks soo much Srikanth and Amit for the quick responses... 

Thanks works now..

Abraham