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
sfdc007sfdc007 

List Index out of Bounds :1 error on button click

Hi ,

I need help on following error

I have a visualforce page with a input field and a button below , when i click the button without giving any value in input field , i am getting the below error

List Index out of Bounds :1
Controller method referred in VF Page :


public PageReference Ok() {
    try {  
    
        string Vz = Vzid;        
        list<user>lstU = new list<user>();  
        system.debug('VZ==>'+Vz);
        lstU = [select vz_id__c, Id, name, email from user where vz_id__c =:Vz];
        if(lstU.size()==0){
           ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Kindly Enter a Valid "VZID" '));
        return null;
        }
        string Name = lstU[0].Name;
        string[] strName = Name.split(' ');
        FirstName = strName[0];
        LastName = strName[1];
        EmailId = lstU[0].email;
    } 
     catch (Exception e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,e.getMessage()));
      return null;
    }
    
  // return page.HackathonRegistrationPage1;
        PageReference pr = new PageReference('/apex/HackathonRegistrationPage2'); 
        pr.setRedirect(false);
        return pr;  
     

  }


It happens on click of "Proceed" button in my VF Page

Kindly help me on it pls
 
MY VF PAGE :

<apex:page standardController="Hackathon_Registration__c" extensions="RegistrationController">



     <apex:includeScript value="{!$Resource.JQUERY_1_9_1}"/>       
        <apex:includeScript value="{!$Resource.HandsOnTableJS}"/>
        <apex:includeScript value="{!URLFOR($Resource.Select2, '/select2/select2.min.js')}"/>
        <apex:includeScript value="{!$Resource.HandsOnTableSelect2}"/>   
        <apex:stylesheet value="{!URLFOR($Resource.Select2, '/select2/select2.css')}"/>
        <apex:stylesheet value="{!$Resource.HandsOnTableCSS}"/>
    <style>  
            body .bPageBlock .detailList tr td, body .bPageBlock .detailList tr th, body table.list tr td, body table.list tr th, body .hoverDetail .bPageBlock .detailList tr td, body .hoverDetail .bPageBlock .detailList tr th {
    border-color: #0C0909;
}

.custom1  {
    background-color: red !important;
}
.custom3 {
    background-color: green !important;
}

.custom2 {
    background-color: orange!important;
}


  </style>
  
  
  
  <apex:sectionHeader title="Visualforce" subtitle="Hackthon Registration"/>

  <apex:form >
    <apex:pageMessages /> <!-- this is where the error messages will appear -->
      <apex:pageBlock mode="edit">
      
      <apex:outputPanel layout="block" styleClass="custom1">
      <apex:pageBlockSection title="Registration Users  Information Details"  columns="1"> 
      </apex:pageBlockSection>
         </apex:outputPanel>  
  <apex:pageBlockSection columns="3">         
             <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Name"></apex:outputLabel>
                    <apex:outputLabel value="{!FirstName}{!LastName}"></apex:outputLabel>
                </apex:pageBlockSectionItem> 
      <apex:pageBlockSectionItem >
                   <!-- <apex:outputLabel value="LastName"></apex:outputLabel>
                    <apex:outputLabel value="{!LastName}"></apex:outputLabel>
                </apex:pageBlockSectionItem> 
      <apex:pageBlockSectionItem > -->
      
                    <apex:outputLabel value="Email"></apex:outputLabel>
                    <apex:outputLabel value="{!eMailId}"></apex:outputLabel>
                </apex:pageBlockSectionItem>
          </apex:pageBlockSection>  
          
    <apex:outputPanel layout="block" styleClass="custom3">       
   <apex:pageBlockSection title=" Please Add Registration Details" columns="1">              
   </apex:pageBlockSection>
    </apex:outputPanel>  
    
   <apex:pageBlockSection columns="1" >
    <apex:pageBlockSectionItem >
            <apex:outputPanel >
            <apex:outputLabel value="{!$ObjectType.Hackathon_Registration__c.Fields.Team_Name__c.Label}"/>          
            </apex:outputPanel>
            <apex:inputField Value="{!HacRegMain.Team_Name__c}"/>
    </apex:pageBlockSectionItem>
    <apex:pageBlockSectionItem >
            <apex:outputPanel >
            <apex:outputLabel value="{!$ObjectType.Hackathon_Registration__c.Fields.PassWord__c.Label}"/>           
            </apex:outputPanel>
            <apex:inputField Value="{!HacRegMain.PassWord__c}"/>
    </apex:pageBlockSectionItem>
    <apex:pageBlockSectionItem >
            <apex:outputPanel >
            <apex:outputLabel value="{!$ObjectType.Hackathon_Registration__c.Fields.Re_Enter_PassWord__c.Label}"/>          
            </apex:outputPanel>
            <apex:inputField Value="{!HacRegMain.Re_Enter_PassWord__c}"/>
    </apex:pageBlockSectionItem>          
   </apex:pageBlockSection>

          <div class="handsontable">
          
                 <apex:outputPanel layout="block" styleClass="custom2">       
          <apex:pageBlockSection title="Add Team Member Details" columns="1" collapsible="false">

        </apex:pageBlockSection>
                  </apex:outputPanel> 
                              <c:Hackathon_Detail_Component id="Haccomp"  Reg_Id="{!RegId}" Table_Name="{!'HackRegistion'}"/>     
         </div>

       <apex:pageBlockButtons location="bottom">    
            <input type="button" onclick="btnClick();" value="Save" id="btnSaveHTML"/> 
    </apex:pageBlockButtons>
<apex:actionFunction name="fnSaveNow" action="{!fnSave}" oncomplete="saveMainGrids();"/> 
<apex:actionFunction name="fnRedirect" action="{!fnRedirect}" />
    </apex:pageBlock>  
      <apex:pageBlock >
<apex:pageBlockSection >

<script>

function saveMainGrids() {  
      tempDisableBeforeUnload = true;
    data1.splice(data1.length-1,1);  
  RegistrationController.savemaingrid('{!RegId}','{!'HackRegistion'}',data1,function (result,event2)
              {
               
                  if(event2.status) 
                  {
                      tempDisableBeforeUnload = true;
                     fnRedirect();
                      
                  } 
              });  
        
      }
     
</script>
 <script>
        function btnClick() {           
             fnSaveNow();
        } 
    </script> 
   
</apex:pageBlockSection>
</apex:pageBlock>

  </apex:form>
</apex:page>



 
Best Answer chosen by sfdc007
Amit Chaudhary 8Amit Chaudhary 8

Please update your code like below. i hope error was coming due to LastName
Controller method referred in VF Page :


public PageReference Ok() {
    try {  
    
        string Vz = Vzid;        
        list<user>lstU = new list<user>();  
        system.debug('VZ==>'+Vz);
        lstU = [select vz_id__c, Id, name, email from user where vz_id__c =:Vz];
        if(lstU.size()==0){
           ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Kindly Enter a Valid "VZID" '));
        return null;
        }
        string Name = lstU[0].Name;
        string[] strName = Name.split(' ');
		If(strName.size() >0)
		{
			FirstName = strName[0];
		}
		if(strName.size() >1)
		{	
			LastName = strName[1];
		}	
        EmailId = lstU[0].email;
    } 
     catch (Exception e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,e.getMessage()));
      return null;
    }
    
  // return page.HackathonRegistrationPage1;
        PageReference pr = new PageReference('/apex/HackathonRegistrationPage2'); 
        pr.setRedirect(false);
        return pr;  
     

  }

Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8

Please update your code like below. i hope error was coming due to LastName
Controller method referred in VF Page :


public PageReference Ok() {
    try {  
    
        string Vz = Vzid;        
        list<user>lstU = new list<user>();  
        system.debug('VZ==>'+Vz);
        lstU = [select vz_id__c, Id, name, email from user where vz_id__c =:Vz];
        if(lstU.size()==0){
           ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Kindly Enter a Valid "VZID" '));
        return null;
        }
        string Name = lstU[0].Name;
        string[] strName = Name.split(' ');
		If(strName.size() >0)
		{
			FirstName = strName[0];
		}
		if(strName.size() >1)
		{	
			LastName = strName[1];
		}	
        EmailId = lstU[0].email;
    } 
     catch (Exception e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,e.getMessage()));
      return null;
    }
    
  // return page.HackathonRegistrationPage1;
        PageReference pr = new PageReference('/apex/HackathonRegistrationPage2'); 
        pr.setRedirect(false);
        return pr;  
     

  }

Let us know if this will help you
 
This was selected as the best answer
sfdc007sfdc007
thanks dude , it worked