Skip to content
Advertisement

Getting java.lang.ExceptionInInitializerError when executing my Test

Here is my code for finding WebElement for all the records in a table

JavaScript

My getter method to access the private WebElement

JavaScript

Here is the function I’ve created to collect all the records in the table in a List

JavaScript

Here is my test execution

JavaScript

Now I’m getting an error java.lang.ExceptionInInitializerError when I’m creating an object jobCategories. Is it an issue with my WebElement declaration? because when I wrote my code like this it executed successfully, but I do not want to do it this was, I want to user the By abstract class

JavaScript

Complete stacktrace

JavaScript

Advertisement

Answer

The explanation is in the exception stacktrace:

JavaScript

The error is in the type-cast in:

JavaScript

You cannot cast a locator to a List. The types are not related.

Instead, you need to call driver.findElements on the By.xpath(...) locator. As explained here:

But a driver value probably isn’t available, so that means this cannot be done at static initialization time. That in turn means that listOfJobs cannot be a static final.

My advice … is to avoid using static fields and methods for this kind of thing.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement