1

I'm trying to run a script via testNG. But getting java.lang.NullPointerException. I have put in the driver initialisation code in BeforeMethod and rest of the code under Test. Please let me know what's wrong in my script due which Im getting the error.Thanks in Advance.

My Script:

public class NewTest {

    WebDriver driver = null;
    @BeforeMethod
    public void beforeMethod() {
        System.setProperty("webdriver.chrome.driver", Constants.Chrome_Driver);
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://" + Constants.auth_Username + ":" + Constants.auth_Password + "@" + Constants.URL);
    }

    @Test
    public void f() throws InterruptedException {
        actions.Admin_Login.txbx_aUsername(driver).sendKeys(Constants.aUsername);
        actions.Admin_Login.txbx_aPassword(driver).sendKeys(Constants.aPassword);
        actions.Admin_Login.btn_login(driver).click();
        actions.create(driver).click();
        Actions action = new Actions(driver);
        action.moveToElement(actions.create.list(driver)).build().perform();


        actions.create_list.list_category(driver).click();
        actions.create_list.list_ceate_category(driver).click();
        actions.create_list.txtbx_Cat_tile(driver).sendKeys(Constants.list_title);
        actions.create_list.btn_Cat_Save(driver).click();
        System.out.println("List Created Successfully");
    }
}

I am using PageObjects to get the data. This script is running fine without testNG. But when I convert the test into testNG and execute, it throws error.

Error I am facing:

java.lang.NullPointerException
    at actions.Admin_Login.txbx_aUsername(Admin_Login.java:13)
    at testScripts.NewTest.f(NewTest.java:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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)

Constants Class :

public class Constants {

    public static final String URL_Shika = "url";

    public static final String list_Title = "Test";

    public static final String aUsername = "Test";

    public static final String aPassword = "abcd123";

    public static final String uUsername = "Test01";

    public static final String uPassword = "abcd123";

    public static final String vUsername = "test321";

    public static final String vPassword = "abcd123";

    public static final String auth_Username = "admin";

    public static final String auth_Password = "admin@123";

    public static final String FF_Driver = "/path/geckodriver";

    public static final String Chrome_Driver = "/path/chromedriver";

}

Action Class:

public class Admin_Login {

        private static WebElement element = null;

        public static WebElement txbx_aUsername (WebDriver driver) {

            element = driver.findElement(By.id("username"));

            return element;
        }

public static WebElement txbx_aPassword (WebDriver driver) {

            element = driver.findElement(By.id("password"));

            return element;
        }

}
4
  • Possible duplicate of What is a NullPointerException, and how do I fix it? Commented Jul 29, 2018 at 18:05
  • Thanks @sidgate. But I'm not getting exactly whats need to be changed in my script to make it work. Commented Jul 29, 2018 at 18:11
  • share your code at Admin_Login.java:13 Commented Jul 29, 2018 at 18:18
  • @sidgate: here is the code: element = driver.findElement(By.id("username")); Commented Jul 29, 2018 at 18:19

1 Answer 1

1

The driver object you are initialising in @BeforeMethod is not visible in your test method. Try following:

public class NewTest {

    WebDriver driver = null;
    @BeforeMethod
    public void beforeMethod() {
        System.setProperty("webdriver.chrome.driver", Constants.Chrome_Driver);
        //Pay close attention this below line. Notice how we are not creating a new WebDriver variable
        //but we are just initialising the class level data member here
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://" + Constants.auth_Username + ":" + Constants.auth_Password + "@" + Constants.URL);
    }

    @Test
    public void f() throws InterruptedException {
        Admin_Login.txbx_aUsername(driver).sendKeys(Constants.aUsername);
        Admin_Login.txbx_aPassword(driver).sendKeys(Constants.aPassword);
        Admin_Login.btn_login(driver).click();
        actions.create(driver).click();
        Actions action = new Actions(driver);
        action.moveToElement(actions.create.list(driver)).build().perform();

        //Create a Course Category
        actions.create_list.list_category(driver).click();
        actions.create_list.list_ceate_category(driver).click();
        actions.create_list.txtbx_Cat_tile(driver).sendKeys(Constants.list_title);
        actions.create_list.btn_Cat_Save(driver).click();
        System.out.println("List Created Successfully");
    }
}

Please try to wait for the text box to appear, before entering any data using following code:

public static WebElement txbx_aUsername (WebDriver driver) {
    WebDriverWait w = new WebDriverWait(driver, 30);
    w.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
    return driver.findElement(By.id("username"));
}

Let me know, if you still face any issue.

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

9 Comments

I have tried your code but still getting the same error. Thanks in advance!
@Kaustubh - I have edited the answer and added a comment to help you notice the change. If you are still facing the same NPE then please edit your question and show us what does Constants class and actions.Admin_Login look like. Because thats the only other place the NPE can arise.
@KrishnanMahadevan: I have updated my question with Constant and Action Class from which I'm pulling data into the Testcase. Please have a look and let me know. Thanks in advance!
Where are you initializing actions object? You do not need to use it for calling txbx_aUsername method. Could you try the updated code and let me know whether it is failing at the same line?
@Mahipal: Yes! It is failing at the same line. I have tried your updated code. Its failing at "Admin_Login.txbx_aUsername(driver).sendKeys(Constants.aUsername);" line.
|

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.