0

I have created this program using TestNG and i'm getting this error java.lan*g.NullPointerException I have initialized the Webelement .But still i'm getting error

POM.java

package TestPOM;
import java.util.concurrent.TimeUnit;
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.PageFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class POM {
 public WebDriver driver;
 @FindBy(how=How.XPATH,using ="//a[@href='/tasks/tasklist.do']")
 public WebElement lnk_MyAccount;
 
 @BeforeClass
 public void openBrowser() {
 System.setProperty("webdriver.chrome.driver","E:\\Users\\codeRefactor\\CodeRefactor\\Jar\\chromedriver_win32\\Chromedriver.exe");
 driver=new ChromeDriver();
 driver.manage().window().maximize();
 driver.get(" url");
 driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
 }
 @BeforeMethod
 public void Login()
 {
 driver.findElement(By.id("username")).sendKeys("admin");
 driver.findElement(By.name("pwd")).sendKeys("manager");
 driver.findElement(By.id("loginButton")).click();
 }
 @Test
 public void createcustomr(){
 PageFactory.initElements(driver, POM.class);
 lnk_MyAccount.click();
 
 }
 
 @AfterMethod
 public void Logout() throws InterruptedException
 {
 Thread.sleep(20000);
 driver.findElement(By.xpath("//a[text()='Logout']")).click();
 }
 
 @AfterClass
 public void closeBrowser()
 {
 driver.close();
 }
 
}

can someone help me on this?

Alexey R.
11.6k5 gold badges21 silver badges39 bronze badges
asked Jun 29, 2020 at 12:56

1 Answer 1

1

When you construct the page using PageFactory.initElements(driver, POM.class); the new instance of the page is created so that the field that you are referring is still null.

What you are doing though is pretty tricky. I would rework your design actually. However changing the mentioned line to PageFactory.initElements(driver, this); might help you..

P.S. - The latter injects the fields into existing object rather than creates a new one of the specified type.

answered Jun 29, 2020 at 13:14
2
  • Thank you so much @Alexey R for you clear explanation! It's working fine Commented Jun 30, 2020 at 7:56
  • Great! Feel free to mark my answer as correct. Commented Jun 30, 2020 at 9:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.