java.lang.NullPointerException with DataProvider

31 views
Skip to first unread message

Rashmi Upari

unread,
Jan 28, 2019, 11:48:15 AM1/28/19
to Selenium Users
hello All,
I am following POM and sending data from DataProvider annotation...and getting java.lang.NullPointerException  error.
Below is the code of Locators, Actions and Main, which are 3 packages having one class each.
Please help me in resolved this

Locators

package locators;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.interactions.Actions;


public class locators

{

public static WebElement reg_click(WebDriver driver)

{

WebElement reg_btn = driver.findElement(By.xpath("//*[@id=\"wrapper\"]/div[1]/div[1]/div[2]/div/span[2]/a"));

return reg_btn;

}

//*****************Personal details**************************//

public static WebElement FirstName(WebDriver driver)

{

WebElement FN = driver.findElement(By.id("input-firstname"));

return FN;

}

public static WebElement LastName(WebDriver driver)

{

WebElement LN = driver.findElement(By.id("input-lastname"));

return LN;

}

}



//*********************************************************************************************


Actions

package actions;


import org.openqa.selenium.WebDriver;


import locators.locators;



public class actions

{

static WebDriver driver;

public static void register(WebDriver driver)

{

locators.reg_click(driver).click();

}

public static void firstname(String firstname)

{

locators.FirstName(driver).sendKeys(firstname);

}

public static void lastname(String lastname)

{

locators.LastName(driver).sendKeys(lastname);

}

}

//***********************************************************************

Main


package main;


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;



public class main 

{

public String baseurl ="http://rumascollection.com.sg/standout";

static WebDriver driver;

@BeforeTest

public void  browseropen() 

{

System.setProperty("webdriver.chrome.driver", "/Users/rashmimohan/Desktop/Selenium_IDE/chromedriver");

WebDriver driver = new ChromeDriver();

driver.get(baseurl);

driver.manage().window().maximize();


}

@Test(dataProvider="getdata", priority=1)

public static void actionmethods(String firstname, String lastname)

{

System.out.println("*******************");

actions.actions.register(driver);

actions.actions.firstname(firstname);

actions.actions.lastname(lastname);

}

@DataProvider

public Object[][] getdata()

{

Object[][] data = new Object[1][2];

data[0][0]="FirstName";

data[0][1]="LastName";

return data;

}

}



Error:

[RemoteTestNG] detected TestNG version 6.14.3

Starting ChromeDriver 2.45.615355 (d5698f682d8b2742017df6c81e0bd8e6a3063189) on port 26590

Only local connections are allowed.

Jan 28, 2019 10:13:36 PM org.openqa.selenium.remote.ProtocolHandshake createSession

INFO: Detected dialect: OSS

*******************

FAILED: actionmethods("FirstName", "LastName")

java.lang.NullPointerException

at locators.locators.reg_click(locators.java:12)

at actions.actions.register(actions.java:13)

at main.main.actionmethods(main.java:36)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.base/java.lang.reflect.Method.invoke(Method.java:566)

at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)

at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)

at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)

at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)

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

===============================================



===============================================

Default suite

Total tests run: 1, Failures: 1, Skips: 0

===============================================


rarunp04

unread,
Jan 28, 2019, 12:20:11 PM1/28/19
to seleniu...@googlegroups.com
Two things:
1. In Main method, you have already declared class variable as WebDriver driver . So in browseropen method reomve the return type as WebDriver (where you have initialized driver)
2.you are not passing driver to firstname and lastname. Please pass the driver to firstname and lastname methods in actions class. It will work

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/f5b0599e-0f9a-4e3b-bc0e-1442047d19d7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rashmi Upari

unread,
Jan 28, 2019, 8:58:02 PM1/28/19
to Selenium Users
Hi,
Thanks for the reply but can you please explain #1 , i did not understand..

Rashmi Upari

unread,
Jan 28, 2019, 9:03:35 PM1/28/19
to Selenium Users
Hey , now i got it!! and its working fine, thanks a ton!! :)
Reply all
Reply to author
Forward
0 new messages