Developing project using TestNG and POM. Without Maven | Selenium Forum
M
Posted on 04/02/2016
When I am trying to login to my application using TestNG class I am getting following error. If I don't use annotations then I am not getting error.

FAILED: LoginAs("rjunnarkar", "Welcome")
java.lang.NullPointerException



Following is the code :

@Test(dataProvider = "dologin")
public void LoginAs(String nUser, String nPass) {
username.sendKeys(nUser);
password.sendKeys(nPass);
Login.click();

PrePay pp = PageFactory.initElements(driver, PrePay.class );
pp.getPrePayment();
}

@DataProvider
public Object[][] dologin() {
return new Object[][] {
new Object[] { "rjunnarkar", "Welcome" },

};
}

@BeforeSuite
public void beforeSuite() {

browser = "Mozilla";
System.out.println(browser);
if(browser.equals("Mozilla"))
driver = new FirefoxDriver();
else if(browser.equals("IE"))
driver = new InternetExplorerDriver();
else if(browser.equals("Chrome")){
driver= new ChromeDriver();
}
System.out.println("Dont give error");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.get(appURL);
}

M
Replied on 04/02/2016

A NullPointerException means that one of the variables you are passing is null, but the code tries to use it like it is not.

[quote:1xwv71oi]If I don't use annotations then I am not getting error.
[/quote:1xwv71oi]

which annotations?


M
Replied on 05/02/2016

getting error in @Test annotation, so where is the problem ?


M
Replied on 05/02/2016

paste entire exception i could not understand the problem and also upload the screen shot.


M
Replied on 11/02/2016

Here is the whole code :

package com.testing;

import org.testng.annotations.Test;
import com.util.Constants;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.BeforeSuite;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterSuite;

public class Connection {

public String appURL = "http://eqtestapp01.ecc.com/";

WebDriver driver = null;
@FindBy(id="loginText")
public WebElement username;
@FindBy(id="pwdText")
public WebElement password;
@FindBy(xpath=Constants.login)
public WebElement Login;
@FindBy(xpath=Constants.Cont)
public WebElement Continue;
@FindBy(xpath=Constants.region)
public WebElement Region;
private String browser;

@Test(dataProvider = "dologin")
public void LoginAs(String nUser, String nPass) {
username.sendKeys(nUser);
password.sendKeys(nPass);
System.out.println(nPass);
Login.click();

//PrePay pp = PageFactory.initElements(driver, PrePay.class );
//pp.getPrePayment();
}

@DataProvider
public Object[][] dologin() {
return new Object[][]{
new Object[]{"rjunnarkar", "abcd"}
};
}
@BeforeTest
public void beforeTest() {
System.out.println("Inside the BeforeTest");
}

@BeforeSuite
public void beforeSuite() {

browser = "Mozilla";
System.out.println(browser);
if(browser.equals("Mozilla"))
driver = new FirefoxDriver();
else if(browser.equals("IE"))
driver = new InternetExplorerDriver();
else if(browser.equals("Chrome")){
driver= new ChromeDriver();
}
System.out.println("Dont give error");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.get(appURL);
}

@AfterSuite
public void afterSuite() {

if(driver!=null){
driver.quit();
System.out.println("Bye Bye");
//driver=null;
}

}

}


Getting error in following lines.
username.sendKeys(nUser);
password.sendKeys(nPass);


M
Replied on 11/02/2016

nuser and npass are empty.

that is why it is giving a null pointer exception.

they're not getting the data from data provider please check them.