String index out of range: 9

Hi ,
I dynamically created the json body . now i am giving the input to field.

for example : i am created multiple same set of field with different value. when tried with count 5 , my code worked . when tried more than 5 , i got this String index out of range: 9
input data :
list of xxCCid= [894607180111007xxxx, 894607180111007xxxx, 894607180111007xxxx, 894607180111007xxxx, 894607180111007xxxx, 894607180111007xxxx, 894607180111007xxxx, 894607180111007xxxx, 894607180111007xxxx]

this is my request : findTestObject(‘CreateOrderRequest/CreateOrder’, [(‘Id’) : GlobalVariable.Id, (‘xyzd’) : GlobalVariable.xyzd, (‘env’) : GlobalVariable.env, (‘subscBody’) : GlobalVariable.subsc, (‘xxCCid’[(count - 1)]) : GlobalVariable.xxCCid[(count - 1)]])

java.lang.StringIndexOutOfBoundsException: String index out of range: 9
at ISL_MultiSaleSale_Max10.run(ISL_MultiSaleSale_Max10:57)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:442)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:433)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:412)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:404)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:281)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:133)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1645739362405.run(TempTestCase1645739362405.groovy:25)

Likely, you have an index, count, that is being “filled” from something, and possibly going to index 10 (as you have count - 1). Review how you “fill” the variable, count.

Please use Code Formatting syntax. Enclose your codes with a pair of triple backtickes. This makes your code better readable.

Hi , here is my code
‘’’’’
@Keyword
def xxxid(count) {

	def slurper = new groovy.json.JsonSlurper()
	while (1 <=count) {
		def response3 = WS.sendRequest(findTestObject('xxxs/xxx/xxx'))

		def result3 = slurper.parseText(response3.getResponseBodyContent())

		String uiccid = result3.sxxCards[0].xxxid

		GlobalVariable.xxxid.add(xxxid)
		println('....' +GlobalVariable.xxxid)
		count--
	}
}

def count = 10

CustomKeywords.‘PreRequest.xxxid’(count)

finalRequest = findTestObject(‘CreateOrderRequest/CreateOrder’, [('trId’) : GlobalVariable.trId, (‘tffId’) : GlobalVariable.tffId, (‘env’) : GlobalVariable.env, (‘subscBody’) : GlobalVariable.subsc, (‘xxxid’[(count - 1)]) : GlobalVariable.xxxid[(count - 1)]])

See my post above.

How about:
while (0 < count) {

and
def count = 9

Please you a pair of backticks.

```

your code here

```

thank you , if i give count 10 ,the error will be “string index out of range:10”. so count is not the issue here …

I add the below code and worked now .but i don’t understand how it is working , i just added while loop without any logic
“”
while(1<=count)
{
count–
}

finalRequest = findTestObject(‘CreateOrderRequest/CreateOrder’, [('t rId’) : GlobalVariable.t rId, (‘tffId’) : GlobalVariable.tffId, (‘env’) : GlobalVariable.env, (‘subscBody’) : GlobalVariable.subsc, (‘xxxid’[(count - 1)]) : GlobalVariable.xxxid[(count - 1)]])

“”"

The first element in a List starts from zero, not 1.
When count comes out of your while loop, it is equal 1. And, “count - 1” therefore is 0. And zero is the first reference in the list.