Null pointer exception in Data provider of TESTNG selenium

Code:

DataProvider.java

package com.suite;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class DataProvider {

public WebDriver driver;
public WebDriverWait wait;
String appURL = “https://www.google.com/intl/en_in/gmail/about/”;
@BeforeTest
public void testSetup() {
System.setProperty(“webdriver.chrome.driver”,”D:/selenium/chromedriver_win32/chromedriver.exe”);
driver=new ChromeDriver();

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

}
@Test(priority=0,dataProvider=”Dataprovider”, dataProviderClass = Data.class)
public void testMethod(String MailSubject) throws InvocationTargetException, InterruptedException ,NullPointerException{
driver.navigate().to(appURL);
WebElement e1=driver.findElement(By.xpath(“html/body/nav/div/a[2]”));
e1.click();

WebElement username = driver.findElement(By.xpath(“.//*[@id=’identifierId’]”));
username.sendKeys(“sharmi.paul87@gmail.com”);

WebElement username_click = driver.findElement(By.xpath(“.//*[@id=’identifierNext’]/content/span”));
username_click.click();
Thread.sleep(1000);
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(“//input[@aria-label=\”Enter your password\”]”)));
WebElement password= driver.findElement(By.xpath(“//input[@aria-label=\”Enter your password\”]”));
password.sendKeys(“**********”);
WebElement password_click = driver.findElement(By.xpath(“.//*[@id=’passwordNext’]/content”));
password_click.click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath(“.//div[normalize-space()=’COMPOSE’]”)).click();
Thread.sleep(1000);
WebDriverWait wait1 = new WebDriverWait(driver,30);
wait1.until(ExpectedConditions.presenceOfElementLocated(By.id(“:ml”)));
driver.findElement(By.id(“:o2”)).sendKeys(“sharmi.paul87@gmail.com”);
driver.findElement(By.id(“:nk”)).sendKeys(MailSubject);
driver.findElement(By.id(“:op”)).sendKeys(“Mailbody”);
driver.findElement(By.xpath(“//*[@id=\”:na\”]”)).click();
}
@AfterTest
public void afterresult(){
driver.close();
}
}

Data.java:

package com.suite;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.testng.annotations.DataProvider;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class Data{

@DataProvider(name=”Dataprovider”)
public static Object[][] Dataprovider() throws IOException, NullPointerException{
Object[][] arrayObject = getExcelData(“D:\\selenium\\Mailsub.xls”,”Sheet1″);
return arrayObject;
}
public static Object[][] getExcelData(String fileName, String sheetName) {
String[][] arrayExcelData = null;

try {
FileInputStream fs = new FileInputStream(fileName);
Workbook wb = Workbook.getWorkbook(fs);
Sheet sh = wb.getSheet(sheetName);

int rows = sh.getRows();

int cols = sh.getColumns();

for (int i =1;i<rows;i++){

String MailSubject = sh.getCell(0, i).getContents();

System.out.println(MailSubject);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}

return arrayExcelData;
}

}

 

TESTNG.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE suite SYSTEM “http://testing.org/testng-1.0.dtd”>
<suite thread-count =”1″verbose =”1″ name=”Gmail suite” annotations=”JDK” >
<test name=”DataProvider”>
<classes>
<class name =”com.suite.DataProvider”/>
<include method=”testMethod”/>
</classes>
<classes>
<class name =”com.suite.Data”/>
<include method=”getExcelData”/>
</classes>

</test>
</suite>

 

Error:

This is sub 1
This is sub 2
This is sub 3
This is sub 4
SKIPPED: testMethod
java.lang.NullPointerException
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:150)
at org.testng.internal.Parameters.handleParameters(Parameters.java:430)
at org.testng.internal.Invoker.handleParameters(Invoker.java:1270)
at org.testng.internal.Invoker.createParameters(Invoker.java:985)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1075)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
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: 0, Skips: 1
===============================================

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

[TestNG] Time taken by org.testng.reporters.EmailableReporter2@6fadae5d: 38 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@5594a1b5: 249 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@6e8dacdf: 1075 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@19dfb72a: 68 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 33 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@2acf57e3: 48 ms

Add Comment
  • 0 Answer(s)
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.