1

I am a beginner to Selenium and I am now using @DataProvider in TestNG framework to pass some values in a webpage (learning purpose). Below is my code:

package Framework;

import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;

public class DataProv {

    WebDriver d;
    @Test(dataProvider = "dp", priority=0)
    public void signin(String uname, String pwd) throws InterruptedException {
        d.findElement(By.linkText("SIGN-ON")).click();
        d.findElement(By.name("userName")).sendKeys(uname);
        d.findElement(By.name("password")).sendKeys(pwd);
        Thread.sleep(3000);
    }

    @Test(dataProvider = "dp1", priority=1)
    public void reg(String fname) throws InterruptedException {
        d.findElement(By.linkText("REGISTER")).click();
        d.findElement(By.name("firstName")).sendKeys("fname");
        d.findElement(By.name("register")).click();
        Thread.sleep(3000);
    }

    @DataProvider
    public Object[][] dp() {
        return new Object[][] {
          new Object[] { "Rachel", "India123" },
          new Object[] { "Rita", "pass123" },
        };
    }

    @DataProvider
    public Object[][] dp1() {
      return new Object[][] {
        new Object[] { "Rachel"},
        new Object[] { "Rita"},
      };
    }
    @BeforeClass
    public void beforeClass() {
        d.manage().window().maximize();
        d = new FirefoxDriver();
        d.get("http://newtours.demoaut.com/");
    }

    @AfterClass
    public void afterClass() {
        d.close();
    }
  }

No, I am getting the following error

FAILED CONFIGURATION: @BeforeClass beforeClass
java.lang.NullPointerException

Can somebody help me in resolving this issue. Thanks in advance

Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85
Devaraj
  • 13
  • 1
  • 4
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – DVK Aug 02 '16 at 16:58

2 Answers2

0

What is Null Pointer Exception?

This issue is coming because you are trying to maximise the browser without it being initialized.

You just have to reverse the order of launching the browser and then maximising it in your @BeforeClass method.

So when you say WebDriver d, reference d is NULL, so when you are trying to call the method manage() on d you basically are trying to call this method using null object i.e. null.manage() thats why Null Pointer Exception so you first have to intantiate it using d = new FirefoxDriver();

d = new FirefoxDriver();
d.manage().window().maximize();
Paras
  • 3,197
  • 2
  • 20
  • 30
0

Check your browser invoking method. I think your driver (d) is not able to find the driver under test . you need to point the driver path before invoking driver == new driver. below is the chrome driver code for your reference.

System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+ "\\src\\test\\resources\\executables\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.manage().window().maximize();
Hemant Varhekar
  • 42
  • 1
  • 1
  • 11