Please let me know the issue with this code , "If " condition is not getting executed properly | Selenium Forum
J
janmejai Gupta Posted on 20/07/2019

package web;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Select_Operations_01 {

public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "F:\\selenium\\selenium software\\driver browsers\\gecko driver\\latest\\geckodriver.exe");
FirefoxDriver fb = new FirefoxDriver();
fb.navigate().to("http://toolsqa.com/automation-practice-form/");
Select oselect = new Select(fb.findElement(By.id("continents")));
//oselect.selectByVisibleText("Africa");
List<WebElement> list = oselect.getOptions();
//int t = list.size();
//System.out.println(t);
int i;
for( i =0 ; i<list.size(); ++i);
{
String o = oselect.getOptions().get(i).getText();
System.out.println(o);

}

//oselect.selectByIndex(1);
//System.out.print(oselect.selectByIndex(1)
Thread.sleep(25);
fb.quit();

}

}

 

this is console error :

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 7, Size: 7
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at web.Select_Operations_01.main(Select_Operations_01.java:25)

 

 

 


A
Ashish Thakur Replied on 22/07/2019

Please check line number 25 for the possible error.

IndexOutOfBoundsException: Index: 7, Size: 7

This ouccurs when your'e trying to provide index greater than size.

For size 7 the maximum index should be 6.


J
janmejai Gupta Replied on 22/07/2019

but I have not given the values anywhere, for the above code for loop is not executing rest of the code is working 

 


A
Ashish Thakur Replied on 23/07/2019

Did you try debugging the code?

As I can see in the code, the issue is in the for-loop.

for( i =0 ; i<list.size(); ++i);
{
String o = oselect.getOptions().get(i).getText();
System.out.println(o);

}

As I can see over here, you have mentioned ";" after loop declaration. Which ends the loop immediately.

Please remove this and the issue will be resolved.