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
gsarguccigsargucci 

Unexplained 'for' loop exception--bug in platform?

Hi,

I have the following code snippet in the managed package for our product:

  ...

List<Area__c> areaName = [Select Id,Name,eoSite__c from Area__c where eoSite__c = : m_project.Site_new__c ORDER BY Name];
  ...

    public List<SelectOption> getAreaOptions()
    {
        List<SelectOption> lstAreaOptions = new List<SelectOption>();
        for (Integer iRow = 0; iRow < areaName.size(); iRow++)
        {

            lstAreaOptions.add(new SelectOption(areaName[iRow].Id, areaName[iRow].Name));
        }
        return lstAreaOptions;
    }

...

When it gets executed in ONE of the orgs where we have our package installed, I get the following exception:

caused by: System.ListException: List index out of bounds: 1
Class.eo3.EditSubContract.getAreaOptions: line 266, column 58

This location corresponds to the line inside the 'for' loop above.  What I don't understand is, the way the code is written, it seems like you should NEVER receive an 'out of bounds' exception, since the list is never modified inside the loop.  The list, BTW, should only contain 1 entry, so it is true that if I tried to access entry at [1], I'd get an exception.  However, shouldn't the 'for' construct handle that?

The package is installed in many orgs; none of which have this problem.  In addition, it seems like the problem started happening about two weeks ago--without any package updates on our part.

So, I am usually the first one to roll my eyes when people say 'it's a bug in the platform', but I'm really not understanding what's happening in this case and how there could possibly be an 'out of bounds' exception in that code.  Has anyone seen anything like that?  Is there some caveat that I'm overlooking?

Going insane--please help!

gsarguccigsargucci

By the way, this is on the NA11 instance...  Thanks!

craigmhcraigmh

That's really weird, but why not just loop through the Area collection?

 

List<Area__c> areaName = [Select Id,Name,eoSite__c from Area__c where eoSite__c = : m_project.Site_new__c ORDER BY Name];
  ...

    public List<SelectOption> getAreaOptions()
    {
        List<SelectOption> lstAreaOptions = new List<SelectOption>();
        for (Area__c area: areaName)
        {

            lstAreaOptions.add(new SelectOption(area.Id, area.Name));
        }
        return lstAreaOptions;
    }

 

gsarguccigsargucci

I agree 100%, but 1) This was someone else's code that I've now modified to do exactly what you describe, but can't quickly test (because it's a managed package installed in a client's production org); and 2) This seemed so strange that I wanted to see if there is something fundamental that I'm missing and should be aware of.

 

thanks,

 

Alex

 

PS: Go Bruins--even though I'm a Sharks fan.  :-)

craigmhcraigmh

I didn't see anything, so you're not crazy, at least as far as this is concerned.

 

I'm going to be at the tank for their game on March 22nd...should be fun wearing my Chara jersey there. :)