Skip to content
Advertisement

I am getting null pointer exception in page object class why I am getting NPE

Base class
package resources;

import java.io.FileInputStream;
import java.io.IOException;
import java.time.Duration;
import java.util.Properties;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;


public class base {

public WebDriver driver;
public Properties property;
public String url= "qwerty"; 


public WebDriver initializeDriver() throws IOException {
    
    
    property = new Properties();
    FileInputStream file = new FileInputStream("D:\qwe\rty\src\main\java\resources\data.properties");
    
    property.load(file);
    String BrowserName = property.getProperty("browser");
     
    if(BrowserName.equals("chrome")) 
    {
     System.setProperty("webdriver.chrome.driver", "D:qwe\rty\chromedriver.exe");   
        driver = new ChromeDriver();
        
    }
    else if(BrowserName.equals("firefox")) 
    {
        driver = new FirefoxDriver();
    }
    else if(BrowserName.equals("IE")) 
    {
        //Executes IE
    }
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
    return driver;
}

public WebDriver verifyPage() {
    
    driver.get(url);
    String Expected = driver.findElement(By.cssSelector("p[class='login-box-msg']")).getText();
    String Actual = "Sign in to start your session";
    Assert.assertEquals(Actual, Expected);
    System.out.println("Homepage is displayed");
    return driver;
}
}

Page Object class

I am getting NPE in user() method in this line return driver.findElement(username);

package pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;


public class LoginPage {

public WebDriver driver;

By username = By.xpath("//input[@type='text']");
By password = By.xpath("//input[@type='password']");
By login = By.xpath("//button[@type='submit']");
By profile = By.xpath("//li[contains(@class,'nav-item dropdown user-menu')]/a[1]/img");
By logout = By.xpath("//li[@class='user-footer']/a[1]");
 



public LoginPage(WebDriver driver) {
    
    this.driver = driver;
 
}


public WebElement user() {

    return driver.findElement(username);
}

public WebElement pass() {

    return driver.findElement(password);
}

public WebElement signIn() {

    return driver.findElement(login);
}

public WebElement pro() {

    return driver.findElement(profile);
}

public WebElement signOut() {

    return driver.findElement(logout);
} 
}

Testcase

When I run testcase when I call lp.user() geeting NPE error in page object class

package Admission;

import java.io.IOException;
import java.time.Duration;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

import pageObjects.LoginPage;
import resources.base;


public class homePage extends base{


public WebDriver driver;
LoginPage lp = new LoginPage(driver);

@Test
public void loginDetails() throws IOException, InterruptedException
{
    
    driver=initializeDriver();
    verifyPage();
    
    
    WebElement aadharNo = lp.user();
    aadharNo.sendKeys("111111111111");
    
    WebElement password = lp.pass();
    password.sendKeys("21102021");
    
    WebElement submit = lp.signIn();
    submit.click();
    
    
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); 
    wait.until(ExpectedConditions.visibilityOf(lp.pro())).click();
    
    WebDriverWait wait1 = new WebDriverWait(driver, Duration.ofSeconds(10)); 
    wait1.until(ExpectedConditions.visibilityOf(lp.signOut())).click();
    
}

    @Test
    public void testCase() {
        
    WebElement aadharNo = lp.user();
    WebDriverWait wait2 = new WebDriverWait(driver, Duration.ofSeconds(30)); 
    wait2.until(ExpectedConditions.visibilityOf(aadharNo));
    aadharNo.sendKeys("111111111111");
    
    WebElement password = lp.pass();
    WebDriverWait wait3 = new WebDriverWait(driver, Duration.ofSeconds(20)); 
    wait3.until(ExpectedConditions.elementToBeSelected(password));
    password.sendKeys("hgfhg");
    lp.signIn();
}
}

java.lang.NullPointerException: Cannot invoke “org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)” because “this.driver” is null at pageObjects.LoginPage.user(LoginPage.java:31) at Admission.homePage.testCase(homePage.java:52)

Advertisement

Answer

You need to initializeDriver() before creating LoginPage object:

public class homePage extends base{
    public WebDriver driver = initializeDriver();
    LoginPage lp = new LoginPage(driver);

    @Test
    public void loginDetails() throws IOException, InterruptedException {
        verifyPage();
        
        WebElement aadharNo = lp.user();
        aadharNo.sendKeys("111111111111");
        
        WebElement password = lp.pass();
        password.sendKeys("21102021");
        
        WebElement submit = lp.signIn();
        submit.click();
        
        
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); 
        wait.until(ExpectedConditions.visibilityOf(lp.pro())).click();
        
        WebDriverWait wait1 = new WebDriverWait(driver, Duration.ofSeconds(10)); 
        wait1.until(ExpectedConditions.visibilityOf(lp.signOut())).click();      
    }

    (...)
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement