while accessing method from another class, i am getting NullPointer exception in selenium.
The method is actually working when try to run it individually but showing exception while accessing the method. i have tried with implicit wait and explicit wait and thread sleep in main class before calling the method but the result is same
//method class
WebDriver driver ;
public void bankId() throws InterruptedException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the id u want to login");
int id = sc.nextInt();
// driver.findElement(By.id("TradeId")).click();
if (id == 1){
System.out.println("welcome to Canara Bank");
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
Select s = new Select(driver.findElement(By.id("TradeId")));
s.selectByValue("1");
}
else if(id == 2){
System.out.println("welcome to Allahabad Bank");
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
Select s = new Select(driver.findElement(By.id("TradeId")));
s.selectByValue("2");
}
else if(id == 3){
System.out.println("welcome to Corporation Bank");
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
Select s = new Select(driver.findElement(By.id("TradeId")));
s.selectByValue("3");
}
else if(id == 4){
System.out.println("CUB Bank");
Thread.sleep(2000);
Select s = new Select(driver.findElement(By.id("TradeId")));
s.selectByValue("4");
}
else {
System.out.println("bank id incorrect, please try again");
}
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
//main class
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "E:\\Automation\\jarfiles2\\chromedriver_win32_new\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("localhost/Role/AdminIntermediary");
//username
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.findElement(By.name("UserName")).sendKeys("admin");
//pass
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.findElement(By.name("Password")).sendKeys("adminadmin");
//login
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[@type='submit']")).click();
Thread.sleep(5000);
driver.navigate().to("localhost/settings");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//getting the list of banks
List<WebElement> options = driver.findElements(By.id("TradeId"));
for (int i=0;i<options.size();i++){
String menu = options.get(i).getText();
System.out.println(menu);
}
//Selecting particular bank
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
GSTlogin gl = new GSTlogin();
gl.bankId();
//stack trace
Exception in thread "main" java.lang.NullPointerException
at GSTlogin.bankId(GSTlogin.java:76)
at bank.main(bank.java:58)