Search code examples
selenium-webdriverwebdriver

Nullpointer Exception in webdriver, driver is null


I am a newbie in selenium java and trying to run test case by calling methods from another class in a framework. Methods are defined in a different class. The issue is that for first instance, the driver is found (Firefox driver) but when a method from another class is called, the driver is null.

Below is the code snippet:


    public class ActionDriver {

    WebDriver driver;

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


    public void type(By loc, String value) {

        driver.findElement(loc).sendKeys(value);

    }
    }

NPE comes for the above line of code for driver for second instance when method is called from the WebAction class. driver.findElement(loc).sendKeys(value);

Second class is :

public class WebActions {

    WebDriver driver;
    ActionDriver ad = new ActionDriver(driver);
    SizeChartTemplate sct = new SizeChartTemplate();

    public void TypeTemplateName(){

        ad.type(jsct.Template_Name, "Men's Vest");
}

}

NPE is for the above line of code at ad.

Test Case:

public class LoginToJcilory extends OpenAndCloseBrowser {


    @Test
    public void logintojcilory() throws BiffException, IOException, InterruptedException{

        WebActions wa = new WebActions();

        System.out.println("Entered login to jcilory block");

        ActionDriver actdrvr = new ActionDriver(driver);

        JciloryLoginPage jclp = new JciloryLoginPage();

        JcilorySizeChartTemplate jsct = new JcilorySizeChartTemplate();

        String username = actdrvr.readExcel(0, 1);

        String password = actdrvr.readExcel(1, 1);

        System.out.println(username);

        System.out.println(password);

        actdrvr.type(jclp.Username, username);

        actdrvr.type(jclp.Password, password);

        actdrvr.click(jclp.LoginButton);

        Thread.sleep(2000);

        driver.get("http://qacilory.dewsolutions.in/JCilory/createSizeChartTemplate.jc");

        wa.TypeTemplateName(); 

    }

NPE comes for wa element in the above code.

Below is the error:

FAILED: logintojcilory
java.lang.NullPointerException
    at Config.ActionDriver.type(ActionDriver.java:40)
    at Config.WebActions.TypeTemplateName(WebActions.java:17)
    at Test.LoginToJcilory.logintojcilory(LoginToJcilory.java:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.access$000(SuiteRunner.java:37)
    at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:368)
    at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)


===============================================
    Test run on FIREFOX
    Tests run: 1, Failures: 1, Skips: 0
===============================================

OpenAndCloseBrowser:

public class OpenAndCloseBrowser {

    protected WebDriver driver;
    @Parameters({"browser","baseURL"})
    @BeforeClass
    public void openBrowser(String browser,String baseURL){
        if(browser.equalsIgnoreCase("firefox")){
            driver=new FirefoxDriver();
        }
        else if(browser.equalsIgnoreCase("chrome")){
            System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\driver\\chromedriver.exe");
            driver=new ChromeDriver();
        }
        else if(browser.equalsIgnoreCase("ie")){
            System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"\\driver\\IEDriverServer.exe");
            DesiredCapabilities caps=new DesiredCapabilities();
            caps.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
            caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);

            driver=new InternetExplorerDriver(caps);
        }
        else{
            driver=new FirefoxDriver();

        }
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
        driver.get(baseURL);    
    }
    @AfterClass
    public void closeBrowser(){
        //driver.quit();
    }

}

I guess there is an issue with the way i am defining the driver in these classes. Any help is resolving the issue is appreciated.


Solution

  • Your driver is null in ActionDriver.type() because it was null when it was passed into the constructor from WebActions. OpenAndCloseBrowser is creating a WebDriver instance, but assigning to a DIFFERENT (local) driver variable. You should learn more about variable scoping in Java...this isn't a Selenium/Webdriver issue.