简体   繁体   English

如何解决“ java.lang.IndexOutOfBoundsException:索引:1,大小:1”的问题

[英]How to fix “java.lang.IndexOutOfBoundsException: Index: 1, Size: 1” problem

I am trying to iterate the elements , where i have to get text body for every element, but after 1st element body is printed, next for next element body I am getting "java.lang.IndexOutOfBoundsException: Index: 1, Size: 1". 我正在尝试迭代元素,在这里我必须获取每个元素的文本主体,但是在打印第一个元素主体之后,接下来是下一个元素主体,我将获得“ java.lang.IndexOutOfBoundsException:Index:1,Size:1” 。 I know this is very simple fix, but i am unable to fix it. 我知道这是非常简单的修复程序,但是我无法修复它。 Please help me to fix this issue. 请帮助我解决此问题。

In my below code, when "String text = KpiText.get(i).getText();" 在我下面的代码中,当“字符串文本= KpiText.get(i).getText();”时 prints for 2nd time i am getting "java.lang.IndexOutOfBoundsException: Index: 1, Size: 1" error. 打印第二次,我得到“ java.lang.IndexOutOfBoundsException:索引:1,大小:1”错误。

public void checkKPIValuesForTeam() throws InterruptedException{


        List<WebElement> userNames = DifferentUsers.findElements(By.xpath("//div[@class='radio sfa-radio-red']"));
        System.out.println(userNames.size());

       int maxLength = userNames.size();

       for(int i=0;i<maxLength;i++){

        WebElement namesOfUsers = userNames.get(i);
        System.out.println(namesOfUsers);
        namesOfUsers.click();

        List<WebElement> KpiText = KPIValues.findElements(By.xpath("//*[@id='main-content-app']/div/div[2]/div/div/div[2]/div[2]/div[1]/div/div[1]"));
        System.out.println(KpiText.size());
        String text = KpiText.get(i).getText();
        System.out.println(text);

}

Expected is it should print the body for all the elements for iteration. 预期它应该为所有元素打印主体以便进行迭代。

The problem is very simple. 问题很简单。 You are using the value "i" to access the "KpiText" array. 您正在使用值“ i”访问“ KpiText”数组。 In this case, your array has only one element, so the index 1 is out of bounds, as the exception stack trace says. 在这种情况下,您的数组只有一个元素,因此索引1超出范围,如异常堆栈跟踪所示。

If you want to print them all you should do this: 如果要全部打印,则应执行以下操作:

for (WebElement element : KpiText)
   System.out.println(element.getText());

It seems that usernames.size() is greater than the size of KpiText. 似乎usernames.size()大于KpiText的大小。 When referencing the KpiText List make sure the index is actually there. 引用KpiText列表时,请确保索引确实存在。 So maybe a check like 所以也许像

if(i < KpiText.size()){
 String text = ...
}

If you are iterating over List<WebElement> KpiText it is better idea to do it like this : 如果要遍历List<WebElement> KpiText ,最好这样做:

   List<WebElement> KpiText = KPIValues.findElements(By.xpath("//*[@id='main-content-app']/div/div[2]/div/div/div[2]/div[2]/div[1]/div/div[1]"));

   for(int i=0; i < KpiText.size() - 1; i++){

    WebElement namesOfUsers = userNames.get(i);
    System.out.println(namesOfUsers);
    namesOfUsers.click();

    System.out.println();
    String text = KpiText.get(i).getText();
    System.out.println(text);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复致命异常:java.lang.IndexOutOfBoundsException 索引:0,大小:1 - How to fix Fatal Exception: java.lang.IndexOutOfBoundsException Index: 0, Size: 1 如何解决java.lang.IndexOutOfBoundsException问题:索引:0,大小:0 - How to solve the problem of java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 java.lang.IndexOutOfBoundsException:索引:1,大小:1 - java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 java.lang.IndexOutOfBoundsException:索引:2,大小:0 - java.lang.IndexOutOfBoundsException: Index: 2, Size: 0 java.lang.IndexOutOfBoundsException:索引:1,大小:1 onBindViewHolder - java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 onBindViewHolder 异常 java.lang.IndexOutOfBoundsException:索引:1,大小:1 - Exception java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 java.lang.IndexOutOfBoundsException:索引:3,大小:3 - java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 java.lang.IndexOutOfBoundsException:索引:0,大小:0? - java.lang.IndexOutOfBoundsException: Index: 0, Size: 0? java.lang.IndexOutOfBoundsException:索引:0,大小:1 - java.lang.IndexOutOfBoundsException: Index: 0, Size: 1 java.lang.IndexOutOfBoundsException:索引:4,大小:4 - java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM