简体   繁体   中英

Selenium Webdriver, How to fix this NullPointer exception?

I am trying to make a List of elements that I can sort through later, but whenever I create the list I get a nullPointer exception.

List<WebElement> tempList = null;
tempList.addAll(driver.findElements(By.className("result-item")));

The HTML page definately has multiple elements with the provided class name, so I'm totally confused. I also get the same exception if I change the code to

tempList.add(driver.findElement(By.className("result-item")));

You set tempList to null and then try to invoke a method on the (not existing) referenced object (because the reference points to null ).

Try the following:

List<WebElement> tempList = new List<>();
tempList.addAll(driver.findElements(By.className("result-item")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM