Forum Discussion

radhika1's avatar
radhika1
Contributor
7 years ago

array out of bound exception.

Hi,

 

 I need to remove  properties in my property step from my 6th line onwards till end .

 

While using the below code I am getting array out of bound exception.please suggest.

 

 

import com.eviware.soapui.SoapUI
def p =testRunner.testCase.getTestStepByName("Properties");
int s = p.getPropertyCount();
log.info (s);
String d = new String[s];
d = p.getPropertyNames();
//log.info d;

//for(o in p.propertyNames)


//{

//log.info(o+""+p.properties[o].value);

//log.info(o +p.properties[o].value);


//}

for (int i =6;i<9;i++)

{
d=(p.propertyNames[i]);
//log.info d;
//p.removeProperty(d));
f= p.properties[d].value;
log.info (f);
p.removeProperty(d);
p.removeProperty(f);

}

 

 

  • Are there 10 properties or 9? Your for loop is trying to go up to the 10th property (because the range is from 0..9).

     

    The line that says p.removeProperty(f) is also suspect.

     

    One of the nifty ideas with Groovy is that you can avoid writing your own code to iterate over all the items in a collection, which is prone to the above type of logic error. Instead, you can use built in features like each, collect, find, findAll, etc.

     

    Here I have used eachWithIndex to achieve the same thing (and I only use an index because of your specific requirement which was given in terms of an index):

     

    testRunner.testCase.getTestStepByName("Properties").with { testStep ->
        testStep.properties.eachWithIndex { property, int propertyIndex ->
            if (propertyIndex >= 6) testStep.removeProperty (property.key)
        }
    }

     

  • JHunt's avatar
    JHunt
    Community Hero

    Are there 10 properties or 9? Your for loop is trying to go up to the 10th property (because the range is from 0..9).

     

    The line that says p.removeProperty(f) is also suspect.

     

    One of the nifty ideas with Groovy is that you can avoid writing your own code to iterate over all the items in a collection, which is prone to the above type of logic error. Instead, you can use built in features like each, collect, find, findAll, etc.

     

    Here I have used eachWithIndex to achieve the same thing (and I only use an index because of your specific requirement which was given in terms of an index):

     

    testRunner.testCase.getTestStepByName("Properties").with { testStep ->
        testStep.properties.eachWithIndex { property, int propertyIndex ->
            if (propertyIndex >= 6) testStep.removeProperty (property.key)
        }
    }