Null Pointer Reference Exception while calling a library

11 views
Skip to first unread message

Manish Bhatia

unread,
Jul 17, 2016, 4:20:06 AM7/17/16
to webdriver
Hi all,
I am trying to login to page by calling a constructor which is saved as a seperate library.
But when this library is called it will throw a Null Pointer Exception
However its launching a page correctly

Below is my code

Login Library
WebDriver driver;
public Login(String UserName,String BrandName)
{
driver=new InternetExplorerDriver();
driver.findElement(By.xpath("//input[@name='UserNameInputText']")).sendKeys(UserName);
driver.findElement(By.xpath("//input[@name='Brand']")).sendKeys(BrandName);
driver.findElement(By.xpath("//input[@name='CmdLogin']")).click();
String Title=driver.getTitle();
if(!Title.contains("VSS 4.0"))
{
System.out.println(UserName+""+"does not exists");
driver.quit();
}
CheckForCancel();
}
private void CheckForCancel() {
if(!driver.findElements(By.id("Cancel")).isEmpty())
{
driver.findElement(By.id("Cancel")).click();
}
}
}

Main Code where the login library is called
import lib.Login;


public class MessageBoard {
void Initalisation()
{

}
public static void main(String[] args)
{
System.setProperty("webdriver.ie.driver", "C:\\Eclipse\\IEDriverServer.exe");
DesiredCapabilities capability=new DesiredCapabilities();
capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
InternetExplorerDriver driver=new InternetExplorerDriver(capability);
Login login=new Login("TYP40FI","Volvo");
}

}

What i am doing wrong here

darrell grainger

unread,
Jul 17, 2016, 7:47:51 AM7/17/16
to webdriver
You are chaining commands. This assumes that everything is successful. This is a bad programming practice. For example,

driver.findElement(By.xpath("//input[@name='UserNameInput']").sendKeys(UserName);

is actually three separate calls. The call to By.xpath is pretty safe and I'd assume it always works. The other two commands are findElement and sendKeys. The sendKeys assumes the findElement work. I suspect one of your findElement calls failed. When this command fails it returns a NULL. If findElement returns a NULL then the sendKeys command is NULL.sendKeys and generating your error.

Bottom line, this isn't really WebDriver other than knowing that findElement returns NULL when it fails. Everything else is just bad Java programming. I can actually spot 5 bad programming practices in your code.
Reply all
Reply to author
Forward
0 new messages