2

I'm getting a java.lang.NullPointerException when running a test case via Selenium webDriver in Chrome (Page factory model)

Testcase:

package Google;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
import Google.OpenGoogle;

public class TestNew {
  @Test
  public void f() throws InterruptedException {


      WebDriver driver = BrowserHandler.BrowserFactory("Chrome");

      OpenGoogle OpenGoogleobj = PageFactory.initElements(driver, OpenGoogle.class);
      OpenGoogleobj.Googletext();

  }
}

Pages:

package Google;

import java.security.PublicKey;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

public class OpenGoogle {

    @FindBy(how = How.XPATH, using = ".//*[@id='lst-ib']")
    WebElement search ;

    static WebDriver driver;


    public OpenGoogle (WebDriver driver)

    {
        this.driver = driver ;

    }

    public void Googletext() throws InterruptedException

    {
        Thread.sleep(4000);
        search.sendKeys("Ankit");


    }

BrowserHandler:

package Google;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class BrowserHandler {

    public static WebDriver driver;


  public static WebDriver BrowserFactory(String BrowserName) {



      if(BrowserName.equals("Chrome"))
      {
          System.setProperty("webdriver.chrome.driver", "/Users/apple/eclipse-workspace/Selenium/src/Google/chromedriver");

      ChromeDriver driver = new ChromeDriver() ;
      driver.manage().window().maximize();

      driver.get("https://google.com");


  }
    return driver;


}
}

TestNG message:

java.lang.NullPointerException

Note: After debugging i am getting problem at " search.sendkeys" m its takine me to some other inbuild class 
[ testResult.setThrowable(ite.getCause());]
  • Could you please provide additional info? It looks like info for "Pages:" is missing. Also, please provide the full StackTrace as this will give us (and you!) information on where NullPointerException occurs, which in turn will help solve it. [Please convert to comment; I don't have sufficient reputation yet] – Marit Mar 4 '18 at 6:54
  • Hi Marit , can you please check now – Batu-QA Mar 4 '18 at 18:20
  • Where and how is search initialized? I see it declared in your code, but it isn't initialized, so any attempt to use it is going to throw a Null Pointer Exception. – Kate Paulk Mar 5 '18 at 13:15
  • OpenGoogle OpenGoogleobj = PageFactory.initElements(driver, OpenGoogle.class); Through this Factory model , driver will initialize the elements present in OpenGoogle class , when this driver will go to OpenGoogle class , it will look for a constructor in OpenGoogle class , now in OpenGoogle class the constructor will initialize all the WebElements – Batu-QA Mar 5 '18 at 14:11
0

Check your BrowserHandler class. You define driver as a static field of the class. Then in BrowserFactory you declare another "local" driver. You instantiate that local object but you return the driver that is a class field which is still null. What you need to do first of all to make your code work is refactor your BrowserHandler so that it looks like:

public class BrowserHandler {

    public static WebDriver BrowserFactory(String BrowserName) {

        System.setProperty("webdriver.chrome.driver", "/Users/apple/eclipse-workspace/Selenium/src/Google/chromedriver");
        ChromeDriver driver = new ChromeDriver() ;

        if(BrowserName.equals("Chrome"))
        {
            driver.manage().window().maximize();
            driver.get("https://google.com");
        }
        return driver;

    }
}

Now your code should work well..

Apart from this particular issue I would strongly recommend you to:

  1. Learn the Java language conventions about how to name methods and variables.
  2. Remove Thread.sleep from Googletext method unless you just need to make sure you have the text entered to the search box. Even if you need to see the text entered you still need to move Tread.sleep so that it is after the sendKeys method method (not before one).
  3. Remove driver field from OpenGoogle class
  4. Remove constructor from OpenGoogle class

Now you have more-or-less well-designed "Hello world" for POM.

-1

You make two webdriver instances in OpenGoogle class and BrowserHandler class, Because of that, webdriver cannot understand which webdriver instance to use for sendkeys action, that's why it gives null point exception.

Create only one webdriver instance to the whole project and use it everywhere you want, the best place is create webdriver instance is beforemethod or beforetest under your TestBase class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.