This is a Main class:
package hrmInfoModule;
public class HrmMain {
public static void main(String[] args) throws Throwable {
Admin_UserMgmt AUser = new Admin_UserMgmt();
AUser.login();
AUser.AdminUsrMgmt();
AUser.AdminAddEmployee();
AUser.AdminDeleteEmployee();
AUser.AdminRecordVerification();
}
}
This is a Parent Class
package hrmInfoModule;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Hrmlogin {
WebDriver driver;
public void login()
{
System.setProperty("webdriver.gecko.driver", "D:\\Software\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
String Url="https://opensource-demo.orangehrmlive.com/";
String ID ="Admin";
String Pswrd="admin123";
driver.get(Url);
WebElement login = driver.findElement(By.id("txtUsername"));
login.sendKeys(ID);
WebElement paswrd = driver.findElement(By.id("txtPassword"));
paswrd.sendKeys(Pswrd);
WebElement btn = driver.findElement(By.id("btnLogin"));
btn.click();
}
}
This is a Child Class
package hrmInfoModule;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
public class Admin_UserMgmt extends Hrmlogin{
WebDriver driver;
public void AdminUsrMgmt() throws Throwable
{
WebElement Admin = driver.findElement(By.xpath(("//*[@id='menu_admin_viewAdminModule']")));
WebElement UseMgmt = driver.findElement(By.xpath("//*[@id='menu_admin_viewAdminModule']/parent::li//following-sibling::ul//li[1]//a[@id='menu_admin_UserManagement']"));
WebElement UseMgmt2 = driver.findElement(By.xpath("//*[@id='menu_admin_viewAdminModule']//following-sibling::ul/li/ul/li/a"));
Actions act= new Actions(driver);
Thread.sleep(2000);
act.moveToElement(Admin).moveToElement(UseMgmt).moveToElement(UseMgmt2).click().build().perform();
}
}
Error
Exception in thread "main" java.lang.NullPointerException
at hrmInfoModule.Admin_UserMgmt.AdminUsrMgmt(Admin_UserMgmt.java:18)
at hrmInfoModule.HrmMain.main(HrmMain.java:9)
Now the problem is whenever I execute main class by object reference variable "AUser", it will not call login method of parent class as well as AdminUsrMgmt method of child class and shown error.
Let me know how to handle exception in thread "main" java.lang.NullPointerException