0

Hi i am trying to click on an element which location is defined using PageFactory.But it is showing NullPointerException.

Locator class:

@FindBy(xpath = "//*[@content-desc = 'Navigate up']")
public By backButton;

PageObject class:

public AskPage()
{
   PageFactory.initElements(driver, AskLocator.class);
}

public void backButtonClick()
{
   if(backButtondisplayed())
      commonactions.clickElement(driver, askLocator.backButton);
}

Am i doing anything wrong?

Note: I have not added classes. I have just added click method and locator for backbutton in page factory class. i have tried changing return type to WebElemment and it works fine. Now i am wondering can i send "By" object from factory class?

  • have you initialize the driver instance ? – NarendraR Apr 10 '17 at 17:43
  • You should spend some time reading the docs on `PageFactory`. You aren't using it correctly. https://github.com/SeleniumHQ/selenium/wiki/PageFactory – JeffC Apr 11 '17 at 03:26
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – JeffC Apr 11 '17 at 03:27
  • @NarendraRajput When i change the return type to WebElement in factory class then element is coming proper but when i am trying to return By object it becomes null. I wanted to get By object as all the methods i have written takes By object as input. Is it possible to send By Object from Factory class? – Rajesh Chaudhary Apr 11 '17 at 06:55
  • @JeffC this question is different from question suggested by you. – Rajesh Chaudhary Apr 12 '17 at 02:09
  • No, it's actually not. You are getting a null pointer exception which is what that question addresses... what they are and how to find and fix them. You need to read it carefully and debug your application to learn where the issue is and to fix it. – JeffC Apr 12 '17 at 04:54

1 Answers1

1

In your code:

You are initialising 'this' class which means you are initialisinf AskPage.class.

PageFactory.initElements(driver, this);

But there is no need to initialise 'AskPage', as you are not locating any web elements.

So the solution is:

PageFactory.initElements(driver, askLocator.class:);

It will do the magic for you now.

Make the web element as static in askLocator.class or create object and access the web element.

Anand Ganesh S S
  • 128
  • 1
  • 2
  • 9
  • Hey thanks. This was one of the mistakes. I corrected this but still element is coming null from factory class. – Rajesh Chaudhary Apr 11 '17 at 06:51
  • When i change the return type to WebElement in factory class then element is coming proper but when i am trying to return By object it becomes null. I wanted to get By object as all the methods i have written takes By object as input. Is it possible to send By Object from Factory class? – Rajesh Chaudhary Apr 11 '17 at 06:54
  • post one of your methods which takes` By` as input. – Kushal Bhalaik Apr 11 '17 at 12:48
  • public static void waitUntilElementIsVisible(WebDriver driver, By element) { WebDriverWait wait = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOfElementLocated(element)); } – Rajesh Chaudhary Apr 12 '17 at 02:06
  • for example above method takes By as input. It does not work when i pass WebElement to it. – Rajesh Chaudhary Apr 12 '17 at 02:07
  • Pleas refer the below link. http://stackoverflow.com/questions/43314243/java-lang-classcastexception-in-web-driverwait-when-using-pagefactory-with-pom?noredirect=1#comment73696067_43314243 – Anand Ganesh S S Apr 14 '17 at 04:05