This question already has an answer here:
This code is a program to real data from string and uses it to test in on the website login page. But while I call the enter_txt(loc, td) function in the class driver_script, I get a NullPointer Exception Error.
I think the problem is in the line -
'dr.findElement(By.xpath(xp)).sendKeys(data);'
in 'All_driver_methods' class. But I couldn't figure out it.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class All_driver_methods {
WebDriver dr;
All_driver_methods(WebDriver dr) {
this.dr = dr;
}
public void enter_txt(String xp,String data)
{
dr.findElement(By.xpath(xp)).sendKeys(data);
}
public void click(String xp)
{
dr.findElement(By.xpath(xp)).click();
}
public void launchchrome(String url)
{
System.setProperty("webdriver.chrome.driver","chromedriver_v78.exe");
WebDriver dr=new ChromeDriver(); //launch the browser
dr.get(url);
}
}
Main Class
public static void main(String[] args) {
// TODO Auto-generated method stub
//login_key ob1=new login_key();
String kw,loc,td;
WebDriver dr=null;
All_driver_methods we=new All_driver_methods(dr);
excel_oper excel=new excel_oper();
for(int r=1;r<=4;r++)
{
kw=excel.read_excel(r,3);
loc=excel.read_excel(r,4);
td=excel.read_excel(r,5);
System.out.println(loc);
System.out.println(kw);
System.out.println(td);
switch(kw)
{
case "launchchrome":
we.launchchrome(td);
break;
case "Enter_txt":
we.enter_txt(loc, td); //Error here
break;
case "click_btn":
we.click(loc);
break;
}
}
}
}