Page Factory is throwing null pointer exception

1,195 views
Skip to first unread message

Madhurya A N

unread,
Feb 14, 2020, 9:08:32 AM2/14/20
to Selenium Users

I have initialized the page factory elements, but it is throwing null pointer error
Login page code:
public class LoginPage extends BaseClass
{
public WebDriver driver;
@FindBy(id="inputusername|input")
public WebElement username;

@FindBy(id="inputppassword|input")
WebElement password;

@FindBy(id="loginbtn")
WebElement loginbutton;

public LoginPage(WebDriver driver)
{
	this.driver = driver;
	PageFactory.initElements(driver, this);

}
public void Login()
{
	System.out.println(username);

Joe Ward

unread,
Feb 14, 2020, 9:53:01 AM2/14/20
to seleniu...@googlegroups.com
What is the "it" that is throwing NullPointerException?

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/40c3e0a5-e77c-4f5d-8810-038fd95bd34c%40googlegroups.com.

Madhurya A N

unread,
Feb 17, 2020, 5:09:07 AM2/17/20
to Selenium Users

The Page Factory elements are not getting stored, when i run the script null pointer error is thrown

Below is my code for LoginPage 

package com.PageFactory;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
import com.TestBase.BaseClass;

@Test
public class LoginPage extends BaseClass
{
public WebDriver driver;
@FindBy(id="inputusername|input")
WebElement username;
@FindBy(id="inputppassword|input")
WebElement password;
@FindBy(id="loginbtn")
WebElement loginbutton;
//LoginPage page = PageFactory.initElements(driver, LoginPage.class);
//BaseClass page = PageFactory.initElements(driver, BaseClass.class);
public LoginPage(WebDriver driver)
{
this.driver = driver;
//PageFactory.initElements(driver, this);
PageFactory.initElements(driver, this);
}
public void Login()
{
System.out.println(username);
username.sendKeys("admin");
}
}

Base Class
package com.TestBase;

import java.io.FileInputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.testng.annotations.Test;

public class BaseClass 
{
public FileInputStream fip;
public Properties prop;
public WebDriver driver;
@Test
    public void LaunchBrowser() throws Throwable
    {
    fip = new FileInputStream(".//src//com//Config//config.properties");
    prop = new Properties();
    prop.load(fip);
    String browserType = prop.getProperty("browser");
    /******** For Chrome Browser ********/
    if(browserType.equalsIgnoreCase("Chrome"))
    {
    System.setProperty(prop.getProperty("chromedriverkey"), prop.getProperty("chromedriver"));
    ChromeOptions options = new ChromeOptions();
options.setBinary(prop.getProperty("chromebrowserpath"));
    driver = new ChromeDriver(options);
    System.out.println("Chrome Browser Launched Successfully!!");
   
    }
    /******** For Firefox Browser ********/
    else if(browserType.equalsIgnoreCase("Firefox"))
    {
    System.setProperty(prop.getProperty("geckodriverkey"), prop.getProperty("geckodriver"));
    FirefoxOptions options= new FirefoxOptions();
options.setBinary(prop.getProperty("firefoxbrowserpath"));
driver = new FirefoxDriver(options);
System.out.println("Firefox Browser Launched Successfully!!");
    }
    /******** Default Chrome Browser ********/
    else
    {
    System.setProperty(prop.getProperty("chromedriverkey"), prop.getProperty("chromedriver"));
    ChromeOptions options = new ChromeOptions();
options.setBinary(prop.getProperty("chromebrowserpath"));
    driver = new ChromeDriver(options);
    System.out.println("Chrome Browser Launched Successfully!!");
    }
    driver.get(prop.getProperty("applicationAccessUrl"));
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }
}


On Friday, February 14, 2020 at 8:23:01 PM UTC+5:30, That Guy wrote:
What is the "it" that is throwing NullPointerException?

On Fri, 14 Feb 2020 at 14:08, Madhurya A N <anmadh...@gmail.com> wrote:

I have initialized the page factory elements, but it is throwing null pointer error
Login page code:
public class LoginPage extends BaseClass
{
public WebDriver driver;
@FindBy(id="inputusername|input")
public WebElement username;

@FindBy(id="inputppassword|input")
WebElement password;

@FindBy(id="loginbtn")
WebElement loginbutton;

public LoginPage(WebDriver driver)
{
	this.driver = driver;
	PageFactory.initElements(driver, this);

}
public void Login()
{
	System.out.println(username);

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to seleniu...@googlegroups.com.

Mike Hetzer

unread,
Feb 17, 2020, 2:15:34 PM2/17/20
to Selenium Users
You still haven't really clarified what is null. 
However I think you have some other fundamental framework issues.

It looks like your "Page Object" is derived from a Base Class that is meant for Tests and is even annotated as a @Test.
Your page objects should derive from a base class intended for page objects.
Frankly, I'm not sure how this code even runs just by looking at it . . .

I think you should check out this guide as a good jumping off point:
https://www.guru99.com/page-object-model-pom-page-factory-in-selenium-ultimate-guide.html

However, PageFactory is getting deprecated, so you probably should not implement it as a design pattern in the long term.
It's still easy to implement the Page Object design pattern without the use of PageFactory too.

I've made a previous post with examples about POM, albeit it is in C# not Java - but the concepts are easily translatable.
https://groups.google.com/d/msg/selenium-users/105ch-hdtLw/vrxB6v4DBQAJ

rarunp04 .

unread,
Feb 17, 2020, 2:26:14 PM2/17/20
to seleniu...@googlegroups.com
Please remove be the webdriver declaration from Login page class
You need to create separate class and define the test annotations not in page objects class

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/2fd383a9-8be6-4d49-a5c1-948517519d68%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages