'Cannot invoke "org.openqa.selenium.By.findElements(org.openqa.selenium.SearchContext)" because "by" is null

I am facing below issue when I try to execute my script Cannot invoke "org.openqa.selenium.By.findElements(org.openqa.selenium.SearchContext)" because "by" is null

Action class method

public static void getWebElementsAndClick(By Locator, String val) { try {

        List<WebElement> optionsToSelect = driver.findElements(Locator);
            for (WebElement option : optionsToSelect) {
                if (option.getText().equals(val)) {
                Actions action1 = new Actions(driver);
                action1.moveToElement(option).click().build().perform();
                driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
                option.click();
                break;
            }
        }
    } catch (NoSuchElementException e) {
        // displayExceptions(e);
        e.getStackTrace();
        System.out.println(e.getStackTrace());
    }
}

Page object class

public class AfterLoginPage extends BaseClass { @FindBy(xpath= "//li[@class='nav-item dropdown customMultiLlevelMenu']//a[@id='navbardrop']") WebElement shopByCategory;

@FindBy(xpath="//ul[@role='menu']//div//a//p")
By shopByCategoryList;

@FindBy(xpath = "//*[@class='dropdown-submenu active ng-star-inserted']//a//a")
WebElement getSecondCategoryList;

@FindBy(xpath="//*[@class='dropdown-menu threeLvlMenu']//li/a/a")
WebElement getThirdCategoryList;

@FindBy(xpath="//a[@class='break curserType']")
WebElement getThirdCategoryProduct;

public AfterLoginPage() {
    PageFactory.initElements(driver, this);
}
public String getCurrentURL() {
    String url= driver.getCurrentUrl();
    return url;
    }
public void clickShopByCategory() throws Throwable  {
    Action.click(driver, shopByCategory);
}
public CategoryTwoPage getShopByCategoryList(String hoverShopByCategoryList) throws Throwable {
    Action.click(driver, shopByCategory);
    Action.implicitWaitCondn(driver, 30);
    Action.moveMouseOver(driver, shopByCategoryList, hoverShopByCategoryList);
    return new CategoryTwoPage();
}

}

Test case

@Test
public void verifyCategoryTwoPageTest() throws Throwable {
    try {
        loginPage = new LoginPage();
        homePage = new HomePage();
        afterLoginPage = new AfterLoginPage();
        categoryTwoPage = new CategoryTwoPage();
        categoryThreePage = new CategoryThreePage();
    
        Thread.sleep(5000);
        loginPage = homePage.clickOnSignIn();
        afterLoginPage = loginPage.enterUserLogin(prop.getProperty("username"), prop.getProperty("password"));
        Thread.sleep(5000);
        categoryTwoPage = afterLoginPage.getShopByCategoryList( "Pneumatic machinery and equipment");
        Thread.sleep(5000);
        //categoryThreePage = categoryTwoPage.clickSecondLvlProduct( "Air fittings and connectors");
        } catch (Exception e) {
        e.printStackTrace(); 
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source