Zeus
Zeus

Reputation: 216

Selenium Nullpointer exception for element

I am trying to pass an element to one of my functions. However, I am getting NullPointer exception for the element.

My Test Class

public class VehicleEdit {
    
      WebDriver driver;
      Elements element = new Elements();

    @When("I search for record")
    public void searchForRecord() throws Exception {
        try {
            driver = DriverFactory.getInstance().getDriver();
            RecordEditPage recordEditPage = new RecordEditPage (driver); 
            recordEditPage.waitToLoad();
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }   
    
}

I also tried printing the text of the element before passing it to waitToBePresentInDom(). The text prints fine that means the element is getting located but still element in waitToBePresentInDom() is shown as null. On debugging I observed that element field in recordInfoSectionHeader() shows data. But in elements.waitToBePresentInDom(driver, this.recordInfoSectionHeader()) it shows second parameter as null. How can it print the data for the same element in the first line of waitToLoad() function but still shows null for the element in the second line of waitToLoad() function?

My Page Object Class

public class RecordEditPage {
    WebDriver driver;
    Elements elements;
    public RecordEditPage(WebDriver driver) {
        this.driver = driver;
    }

    public WebElement recordInfoSectionHeader() {
        WebElement element = driver.findElement(By.id("record-id_header"));
        System.out.println("Printing the element's text " + element.getText());
        return element;
    }

        public void waitToLoad() throws Exception{
            System.out.println("Printing the element's text " + this.recordInfoSectionHeader().getText()); \\This is printing the text properly
            elements.waitToBePresentInDom(driver, this.recordInfoSectionHeader(), 25);
          } 
}

Elements class

public class Elements {
    public void waitToBePresentInDom(WebDriver driver, WebElement element, int secondsDelay) throws Exception {
        try {
            WebDriverWait wait = new WebDriverWait(driver, secondsDelay);
            wait.until(ExpectedConditions.visibilityOf(element) );
        } catch (Exception e) {
            if ((ExceptionUtils.indexOfThrowable(e, TimeoutException.class) != -1)
                    || (ExceptionUtils.indexOfThrowable(e, NoSuchElementException.class) != -1)) {
                e = new Exception(element + " failed to appear!!!\n\n");
            }
            throw e;
        }
    }
}

Error that I am getting:

java.lang.NullPointerException: Cannot invoke "com.utilities.Elements.waitToBePresentInDom(org.openqa.selenium.WebDriver, org.openqa.selenium.WebElement, int)" because "this.element" is null

Update When I moved waitToBePresentInDom() function to my Page Object it worked fine. However, I do not want to keep waitToBePresentInDom() in my Page Object class but in a separate class so that all Page Objects can use waitToBePresentInDom().

Upvotes: 0

Views: 82

Answers (1)

Zeus
Zeus

Reputation: 216

I figured out the problem with my code. Answering here so that if anyone faces same issue then this may help them. I declared Elements elements; but never initialized it in RecordEditPage class. After I changed it to Elements elements = new Elements(); it worked. The error message was so deceptive!

Upvotes: 1

Related Questions