Reputation: 19
I want to add the elements(Veg/Fruit name) into a List and want to print the list by clicking on "Next" Button, but I am getting error as: java.lang.NullPointerException
, at the line veggieList1.add(element);
CODE:
public void seleniumStream1() {
System.setProperty("webdriver.chrome.driver", "C:\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://rahulshettyacademy.com/seleniumPractise/#/offers");
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement next1 = driver.findElement(By.cssSelector("[aria-label='Next']"));
List<WebElement> veggieList1 = null;
for (int i = 0; i < 4; i++) {
List<WebElement> veggies1 = driver.findElements(By.xpath("//tbody/tr/td[1]"));
for (WebElement element : veggies1) {
veggieList1.add(element);
}
if (next1.getAttribute("aria-disabled").equalsIgnoreCase("false")) {
next1.click();
}
}
for (WebElement element1 : veggieList1) {
System.out.println(element1.toString());
}
driver.close();
}
Upvotes: -1
Views: 1001
Reputation: 1789
You are initializing the veggieList1
to null
on line List<WebElement> veggieList1 = null;
. You should use List<WebElement> veggieList1 = new ArrayList<>();
if you wish to initialize this variable with an empty list.
Upvotes: 0