0

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

Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
  • You should 'handle' NullPointerException you should prevent them from occurring by not dereferencing null values. – Mark Rotteveel Aug 12 '20 at 10:41
  • It's difficult to tell you which line is failling as you don't provide accurate line numbers - but i think Your driver in `public void AdminUsrMgmt()` is null. When you try and run `WebElement Admin = driver.findElement(....` it's telling you it's pointed to null.. Your code needs a bit of work, but a QUICK solution (not the best, but quick) is in `class Hrmlogin` create a new method to GetDriver, which will return driver from that parent to wherever it's needed. Then, in `public void AdminUsrMgmt()` - call that GetDriver before your first line to get the value.. – RichEdwards Aug 12 '20 at 10:57
  • POsting it as a comment as i can't test it - it's just a static review. If it needs more info let me know and i'll test it and upgrade it to an answer when i can – RichEdwards Aug 12 '20 at 10:59

0 Answers0