Search code examples
seleniumnullpointerexceptionpojo

when i am using pojo, i am getting exception?


hi all please check my code

public class Sample1 extends Sample {
        public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\Balaji-PC\\cucumber\\SamplePro\\driver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.facebook.com/");
        driver.manage().window().maximize();

        Sample s= new Sample1();
        WebElement d = s.getUsername();

        d.sendKeys("lsmanikandan");
        s.getPassword().sendKeys("manikandan");
    }
}

Please check below my pojo class

public class Sample {
    WebDriver driver;
    public Sample() {
        PageFactory.initElements(driver, this);
    }

    @FindBy(id = "email")
    private WebElement username;

    @FindBy(id = "pass")
    private WebElement password;

    public WebElement getUsername() {
        return username;
    }

    public WebElement getPassword() {
        return password;
    }

}

please find below the exception

Exception in thread "main" 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.$Proxy4.sendKeys(Unknown Source) at org.test.Sample1.main(Sample1.java:18)


Solution

  • The issue in your code is that you are not passing your driver state from child class Sample 1 to based class Sample.

    The magic is in these three points

    1. Adding parametrized constructor in Child class - Sample1

      public Sample1(WebDriver driver) { super(driver); }

    2. Adding parametrized constructor in Parent Class - Sample

      public Sample(WebDriver driver) { PageFactory.initElements(driver, this); System.out.println("Page Factory started"); }

    3. Creating object to called page factory in Parent class by passing driver.

      Sample s = new Sample1(driver);

      public class Sample {
      WebDriver driver;
      public Sample(WebDriver driver) {
          PageFactory.initElements(driver, this);
          System.out.println("Page Factory started");
      }
      
      @FindBy(id = "email")
      private WebElement username;
      
      @FindBy(id = "pass")
      private WebElement password;
      
      public WebElement getUsername() {
          System.out.println(username.getAttribute("data-testid"));
          return username;
      }
      
      public WebElement getPassword() {
          System.out.println(username.getAttribute("data-testid"));
          return password;
      }  }
      
      public class Sample {
          WebDriver driver;
      public Sample(WebDriver driver) {
          PageFactory.initElements(driver, this);
          System.out.println("Page Factory started");
      }
      
      @FindBy(id = "email")
      private WebElement username;
      
      @FindBy(id = "pass")
      private WebElement password;
      
      public WebElement getUsername() {
          System.out.println(username.getAttribute("data-testid"));
          return username;
      }
      
      public WebElement getPassword() {
          System.out.println(username.getAttribute("data-testid"));
          return password;
      }    }
      

    enter image description here