vinay
vinay

Reputation: 13

Getting error when using FindBy in selenium. Error message null point exception

import com.sun.javafx.PlatformUtil;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class HotelBookingTest {
    WebDriver driver;
    @FindBy(xpath= "//*[@class='hotelApp ']")
    public static WebElement hotelLink;


    @Test
    public void shouldBeAbleToSearchForHotels() {
        setDriverPath();
        driver = new ChromeDriver();
        driver.get("https://www.cleartrip.com/");
        boolean hotelLinkDisplayed = hotelLink.isDisplayed();

       hotelLink.click();
        driver.quit();

    }
}

Getting error on line "HotelLink.click" and that hotelLink element is defined using findBy annotation but getting "java.lang.NullPointerException" error

Upvotes: 1

Views: 1595

Answers (3)

Adarsh J
Adarsh J

Reputation: 61

Since you are using @FindBy annotation you have to initialize the element before using.

you can do that by creating parameterized constructor accepting WebDriver type as argument.

        PageFactory.initElements(driver, this);```

and call this constructor after opening the browser.
i.e after this line 
```driver = new ChromeDriver();```

Upvotes: 1

cheparsky
cheparsky

Reputation: 530

For @FindBy annotations you need to implemet it before searching of WebElement.

You can add method that do it for you in a simple way:

import com.sun.javafx.PlatformUtil;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

import org.openqa.selenium.support.PageFactory;

public class HotelBookingTest {
    WebDriver driver;
    @FindBy(xpath= "//*[@class='hotelApp ']")
    public static WebElement hotelLink;


    @Test
    public void shouldBeAbleToSearchForHotels() {
        setDriverPath();
        driver = new ChromeDriver();
        HotelBookingTest.setPageObject(driver);
        driver.get("https://www.cleartrip.com/");
        boolean hotelLinkDisplayed = hotelLink.isDisplayed();

       hotelLink.click();
        driver.quit();

    }

    public static void setPageObject (WebDriver wd) {
        PageFactory.initElements(wd, new HotelBookingTest ());
    }
}

Upvotes: 2

Ali
Ali

Reputation: 1689

As you are using @FindBy annotations, you need to initialize all the web elements before using it.

Create a construct for HotelBookingTest class and initialize using PageFactory like below :

import com.sun.javafx.PlatformUtil;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class HotelBookingTest {
    WebDriver driver;

    @FindBy(xpath= "//*[@class='hotelApp ']")
    public WebElement hotelLink;

    public HotelBookingTest(WebDriver driver) {
        PageFactory.initElements(driver, this);
    }

    @Test
    public void shouldBeAbleToSearchForHotels() {
        setDriverPath();
        driver = new ChromeDriver();
        new HotelBookingTest(driver);
        driver.get("https://www.cleartrip.com/");
        boolean hotelLinkDisplayed = hotelLink.isDisplayed();

        hotelLink.click();
        driver.quit();
    }
}

Import PageFactory from corresponding package and remove static before the `hotelLink.

I hope it helps...

Upvotes: 3

Related Questions