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

private final static List<WebElement> listOfJobs = 
        (List<WebElement>) By.xpath("//*[@id='resultTable']//tbody/tr//a");

My getter method to access the private WebElement

public static List<WebElement> getListOfJobs() 
{
   return listOfJobs;
}

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

    @SuppressWarnings("null")
    public static List<String> listOfJobs()
    {
        List<String> jobs = null;
        for(int i=0; i < OrangeHRMAddJobCategories.getListOfJobs().size(); i++)
        {
            jobs.add(OrangeHRMAddJobCategories.getListOfJobs().get(i).getText());
        }
        return jobs;
    }

Here is my test execution

        OrangeHRMAddJobCategories jobCategories = new OrangeHRMAddJobCategories();
        jobCategories.clickJobTab().clickJobCategoires().clickAdd().setJobCategoryName(map.get("categoryname")).saveJobCategory();
        
        Assertions.assertThat(UsefulFunctionUtils.listOfJobs().contains(map.get("categoryname")));

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

public static List<WebElement> listOfJobs = driver.findElements(By.xpath(("//*[@id='resultTable']//tbody/tr//a")));

public static List<WebElement> getListOfJobs() 
    {
        return listOfJobs;
    }

Complete stacktrace

java.lang.ExceptionInInitializerError
    at com.digicorp.testcases.TC_AddJobCategory.testAddJobCategory(TC_AddJobCategory.java:27)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.testng.TestRunner.privateRun(TestRunner.java:794)
    at org.testng.TestRunner.run(TestRunner.java:596)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
    at org.testng.SuiteRunner.run(SuiteRunner.java:276)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
    at org.testng.TestNG.runSuites(TestNG.java:1063)
    at org.testng.TestNG.run(TestNG.java:1031)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.ClassCastException: class org.openqa.selenium.By$ByXPath cannot be cast to class java.util.List (org.openqa.selenium.By$ByXPath is in unnamed module of loader 'app'; java.util.List is in module java.base of loader 'bootstrap')
    at com.digicorp.pageobjects.OrangeHRMAddJobCategories.<clinit>(OrangeHRMAddJobCategories.java:19)
    ... 29 more

Advertisement

Answer

The explanation is in the exception stacktrace:

class org.openqa.selenium.By$ByXPath cannot be cast to class java.util.List

The error is in the type-cast in:

private final static List<WebElement> listOfJobs = 
    (List<WebElement>) By.xpath("//*[@id='resultTable']//tbody/tr//a");

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