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
mritzmritz 

Error: Subscript is invalid because list is empty

Apex Code:

public class myClass{
        public list<String> myValues{get;set;}
        public string outTxt{get;set;}
        public myClass(){
            outTxt='here-> ';
            myValues=new list<String>();
         }
         public void concatenate(){
              
              for(integer i=0;i<4; i++)
                    outTxt+=myValues[i]+'\n';
         }
}


VisualForce Page Code:

<apex:page controller="myClass">
    <apex:form >
        <table>
            <tr>
                <td>Value1:</td>
                <td><apex:inputtext value="{!myValues[0]}" id="val1"/></td>
            </tr>
            <tr>
                <td>Value2:</td>
                <td><apex:inputtext value="{!myValues[1]}" id="val2"/></td>
            </tr>
            <tr>
                <td>Value3:</td>
                <td><apex:inputtext value="{!myValues[2]}" id="val3"/></td>
            </tr>
            <tr>
                <td>Value4:</td>
                <td><apex:inputtext value="{!myValues[3]}" id="val4"/></td>
            </tr>
            <tr>
                <td><apex:commandButton value="Concatenate" action="{!concatenate}">
                        <apex:ActionSupport event="onclick" rerender="txt"/>
                    </apex:commandButton>
                </td>
                
            </tr>
        </table>
        <apex:outputText id="txt">{!outTxt}</apex:outputText>
    </apex:form>
</apex:page>




The problem is that when i save this visualforce page code after saving apex code i am getting error:
Subscript is invalid because list is empty
Best Answer chosen by mritz
ManojjenaManojjena
Hi Mritiz,
Please replace your class with below code it will work .
public class myClass {
    public list<String> myValues{get;set;}
        public string outTxt{get;set;}
        public myClass (){
            outTxt='here-> ';
            myValues=new list<String>{'','','',''};
            
         }
         public void concatenate(){
             for(integer i=0;i<4; i++)
                    outTxt+=myValues[i]+'\n';
         }
}

Thanks 
Manoj
 

All Answers

ManojjenaManojjena
Hi Mritzi,
Currently your list is  empty .What actually you want do display in your page ?
mritzmritz
I want to save values from all input text to this list and then save it all in one string.
ManojjenaManojjena
Hi Mritiz,
Please replace your class with below code it will work .
public class myClass {
    public list<String> myValues{get;set;}
        public string outTxt{get;set;}
        public myClass (){
            outTxt='here-> ';
            myValues=new list<String>{'','','',''};
            
         }
         public void concatenate(){
             for(integer i=0;i<4; i++)
                    outTxt+=myValues[i]+'\n';
         }
}

Thanks 
Manoj
 
This was selected as the best answer
mritzmritz
What is significance of 4 null strings?
myValues=new list<String>{'','','',''};

What in case i have 10s of such fields, do i need to have equal no of null strings?

or is there any better approach.
ManojjenaManojjena
Hi Mritiz,

Yes you need to instantiate the list with 10 number of null string . Else you can create different different variable for each field and do addition in method .But it will be selected as best approach based on your requirement .



 
mritzmritz
@Manoj,

I modified the constructor a bit and its like:
 
public class myClass {
    public list<String> myValues{get;set;}
        public string outTxt{get;set;}
        public myClass (){
            outTxt='here-> ';
            myValues=new list<String>(5);
            for(integer i=0;i<5;i++)
                   myValues[i]='';
            
         }
         public void concatenate(){
             for(integer i=0;i<4; i++)
                    outTxt+=myValues[i]+'\n';
         }
}