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
aamDevaamDev 

List index out of bounds

I'm receiving the message "Content cannot be displayed: List index out of bounds: 0" .

 

I have a selectlist whose List<SelectOption> is coming up empty. In the event it does come up empty, I would like to pass my own message through the VF page, something to the effect of "No Production Numbers currently available". How do I do that?

 

Piece of form in question:

<apex:selectList id="productionNumbers" value="{!productionNumbers}" multiselect="true" size="4">
    <apex:selectOptions value="{!productionOptions}" />
</apex:selectList>

 Controller:

String[] productionNumbers = new String[]{};

List<Production_No__c> availableProd = [SELECT Id, Name, Branch__c FROM Production_No__c WHERE Branch__c IN : accId AND Id NOT IN : selectedProd];

public List<SelectOption> getProductionOptions() {
		
    List<SelectOption> options = new List<SelectOption>();
		
    for (Production_No__c productionNo : availableProd) {
			
	options.add(new SelectOption(productionNo.Id, productionNo.Name));
			
    }
		
    return options;
		
}
	
public String[] getProductionNumbers() {
		
    return productionNumbers;

}
	
public void setProductionNumbers(String[] productionNumbers) {
		
    this.productionNumbers = productionNumbers;
		
}

 

Thank you!

Best Answer chosen by Admin (Salesforce Developers) 
aamDevaamDev

To avoid this message, I took out the availableProd list, removed the reference to the List availableProd in my getProductionOptions and wrote out the original query:

 

public List<SelectOption> getProductionOptions() {
		
	List<SelectOption> options = new List<SelectOption>();
		
	for (Production_No__c productionNo : [SELECT Id, Name, Branch__c FROM Production_No__c WHERE Branch__c IN : accId AND Id NOT IN : selectedProd]) {
			
			options.add(new SelectOption(productionNo.Id, productionNo.Name));
			
		}

	return options;
		
}

 

To be able render the list as false, I put the original query in an ApexPages.Standardsetcontroller and set the .getResultSize to a public integer:

 

ApexPages.Standardsetcontroller ap = new ApexPages.Standardsetcontroller(Database.getQueryLocator([SELECT Id FROM Production_No__c WHERE Branch__c IN : accId AND Id NOT IN : selectedProd]));

public Integer I {

	get { return ap.getResultSize(); }
	set { this.i = i; }
		
}

 

 

On the page:

 

<apex:selectList id="productionNumbers" styleClass="firstSelect" value="{!productionNumbers}" multiselect="true" size="4" rendered="{!(i > 0)}">
	<apex:selectOptions value="{!productionOptions}" />
</apex:selectList>

 

 

This may be a ridiculous hack, but it works. I'm open to suggestions, but if it's cool I keep it as is. Thanks again

All Answers

SurekaSureka

Hi,

 

Your requirement is, if the Production_No query is not fetching any rows, you need to display the message, else display the query rows. Is that right?

 

If so, include a parameter "rendered" in  the "SelectList". ie.

 

<apex:selectList id="productionNumbers" value="{!productionNumbers}" multiselect="true" size="4" rendered={!bool}">

 

Declare a boolean variable in the apex class.

 

in the getProductionOptions() method of apex class,  check for the condition

 

if(availableProd.size()==0)
bool = false;
else
{
bool = true;

}

 

Similarly declare an output text in the visual force page, and render the output text in the same way through boolean and display your message in the output text.

 

Let me know, if that works.

 

Thanks

aamDevaamDev

Thanks for your suggestion. Unfortunately, I'm still getting the same message. I'm under the impression that I will receive the out of bounds message regardless of what I put under rendered in the selectlist. Prior to making this post, I tried to tie the rendered attribute to the list itself and then to its options productionOptions, and received the same error:

 

rendered="{!NOT(ISNULL(productionNumbers))}"
or
rendered="{!NOT(ISNULL(productionOptions))}"

Am I wrong in assuming that I will get this message regardless of the list's attributes? I appreciate your help.

aamDevaamDev

To avoid this message, I took out the availableProd list, removed the reference to the List availableProd in my getProductionOptions and wrote out the original query:

 

public List<SelectOption> getProductionOptions() {
		
	List<SelectOption> options = new List<SelectOption>();
		
	for (Production_No__c productionNo : [SELECT Id, Name, Branch__c FROM Production_No__c WHERE Branch__c IN : accId AND Id NOT IN : selectedProd]) {
			
			options.add(new SelectOption(productionNo.Id, productionNo.Name));
			
		}

	return options;
		
}

 

To be able render the list as false, I put the original query in an ApexPages.Standardsetcontroller and set the .getResultSize to a public integer:

 

ApexPages.Standardsetcontroller ap = new ApexPages.Standardsetcontroller(Database.getQueryLocator([SELECT Id FROM Production_No__c WHERE Branch__c IN : accId AND Id NOT IN : selectedProd]));

public Integer I {

	get { return ap.getResultSize(); }
	set { this.i = i; }
		
}

 

 

On the page:

 

<apex:selectList id="productionNumbers" styleClass="firstSelect" value="{!productionNumbers}" multiselect="true" size="4" rendered="{!(i > 0)}">
	<apex:selectOptions value="{!productionOptions}" />
</apex:selectList>

 

 

This may be a ridiculous hack, but it works. I'm open to suggestions, but if it's cool I keep it as is. Thanks again

This was selected as the best answer