1

I am trying to automate using web driver,testng and page factory. But I am facing null pointer exception. Below is the code for the same.

HomePage page object class

This is page factory class

package POM;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class HomePage 
{       
    WebDriver driver;
    private static WebElement element = null;

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

    public static WebElement signin(WebDriver driver)
    {
        element=driver.findElement(By.xpath("/html/body/div/div[1]/header/div[2]/div/div/nav/div[1]/a"));
        return element;
    }

    public static WebElement emailCreate(WebDriver driver)
    {
        element=driver.findElement(By.id("email_create"));
        return element;
    }

    public static WebElement submitCreate(WebDriver driver)
    {
        driver.findElement(By.id("SubmitCreate"));
        return element;
    }

}

This is Test Class for executin actual test. Test case class

package POM;

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

import POM.HomePage;

public class MyTest {


    private static WebDriver driver = null;

  @BeforeMethod
  public void beforeMethod() 
  {

        System.setProperty("webdriver.chrome.driver", "C:/chromedriver_win32/chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.get("http://www.automationpractice.com");
        System.out.println("Hi");
        driver.manage().window().maximize();  
  }

  @Test
  public void login() throws InterruptedException 
  {
      System.out.println("login");
      HomePage.signin(driver).click();
      String title = driver.getTitle();
      System.out.println(title);
      HomePage.emailCreate(driver).sendKeys("[email protected]");
      Thread.sleep(2000);
      HomePage.submitCreate(driver).click();

  }

  @AfterMethod
  public void afterMethod() 
  {
      driver.close();
  }

}

Exception I am getting below exception.

[RemoteTestNG] detected TestNG version 6.14.2
Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 42928
Only local connections are allowed.
Apr 19, 2018 2:48:27 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Hi
login
FAILED CONFIGURATION: @AfterMethod afterMethod
java.lang.NullPointerException
    at POM.MyTest.afterMethod(MyTest.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:455)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

FAILED: login
java.lang.NullPointerException
    at POM.HomePage.signin(HomePage.java:16)
    at POM.MyTest.login(MyTest.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
    Configuration Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================

Can anyone please help me resolve this error?

2 Answers 2

2

it seems that driver == null, that is why you got those exceptions.

Try replacing

private static WebDriver driver = null;

By

private static WebDriver driver = new ChromeDriver();

Or in your beforeMethod replacing

WebDriver driver=new ChromeDriver();

By

driver=new ChromeDriver();

because you're actually creating a second driver variable.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. It helped and it's working. I made a silly mistake..:P
1

The easiest solution is you should not declare/create the "driver" object twice.

in your test case you have created driver object twice

1st - Class variable: private static WebDriver driver = null;

2nd - in @Before method: WebDriver driver=new ChromeDriver();

remove the "WebDriver" from second instance and let it to be "driver = new ChromeDriver();"

That's all, your null pointer exception will disappear and code will execute.

Comments

Your Answer

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.