In the project I have created 3 classes. First class is Browser_Initialize in src/main/java, second class is PhoneWall_Page in src/main/java, third class is PhoneWall_TestClass in src/main/java. In Browser_Initialize I have defined browser initiation. In PhoneWall_Page initialized all webelemnts using Page Factory & also created few methods. In PhoneWall_TestClass calling methods present in Phonewall_Page class. Once code reaches @Test anbnotation, I am getting Null pointer Exception.
package com.base;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BaseClass {
public static Properties prop;
public static WebDriver driver;
public BaseClass() {
try {
prop=new Properties();
FileInputStream fip=new FileInputStream(System.getProperty("user.dir")+"//src/main//resources//PropertyFile//PropertyData.properties");
prop.load(fip);
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
}
public void initialize() {
System.setProperty("webdriver.chrome.driver", "C:\\chromeDriver\\chromedriver_win32 (2)\\chromedriver.exe");
driver=new ChromeDriver();
driver.get(prop.getProperty("url"));
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println(driver);
}
}
package com.pages;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.base.BaseClass;
public class PhonePage extends BaseClass{
WebDriver driver;
public PhonePage(WebDriver driver) {
//super(driver);
this.driver=driver;
PageFactory.initElements(driver, this);
}
@FindBy(xpath="//div[@aria-label='View device Apple iPhone 12 Pro Max ']")
WebElement Device;
public void ScrollMethod() {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,1000)");
}
public void DeviceSelection() {
System.out.println("Device selection");
System.out.println(driver);
Device.click();
}
}
package com.test;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.base.BaseClass;
import com.pages.PhonePage;
public class PhoneTest extends BaseClass {
WebDriver driver;
PhonePage phonepage=new PhonePage(driver);;
//
// public PhoneTest(WebDriver driver) {
////// super(driver);
// this.driver=driver;
// }
@BeforeMethod
public void browser() {
System.out.println("Before method driver obj is:"+ driver);
initialize();
System.out.println("Before method driver obj is:"+ driver);
}
@Test
public void SelectDevice() {
System.out.println("Test mthod driveris:"+driver);
phonepage.DeviceSelection();
}
@AfterTest
public void closeBrowser() {
driver.close();
}
}
Error displayed in console:
java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy11.click(Unknown Source)
at com.sprint.qa.pages.PhoneWall_Page.DeviceSelection(PhoneWall_Page.java:31)
at com.sprint.qa.testcases.PhoneWall_TestClass.SelectDevice(PhoneWall_TestClass.java:31)