Getting "java.lang.NullPointerException" exception when i try to run my POM test



  • I have 2 classes in my project

    First class contains all the webelements

    Second one for method where i am using this elements

    package facebook;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    
    public class webelement {
    
    
        private static WebElement element;
    
    
        public static WebElement firstname(WebDriver driver) {
            driver.findElement(By.xpath("//input[@name='firstname']"));
            return element; 
        }
        public static WebElement lastname(WebDriver driver)
        {
            driver.findElement(By.xpath("//input[@name='lastname']"));
            return element;
        }
        public static WebElement email(WebDriver driver)
        {
            driver.findElement(By.xpath("//input[@name='reg_email__']"));
            return element;
        }
        public static WebElement newpassword(WebDriver driver)
        {
            driver.findElement(By.xpath("//input[@name='reg_passwd__']"));
            return element;
        }
        public static WebElement day(WebDriver driver)
        {
            driver.findElement(By.xpath("//select[@name='birthday_day']"));
            return element;
        }
    
        public static WebElement month(WebDriver driver)
        {
            driver.findElement(By.xpath("//select[@name='birthday_month']"));
            return element;
    
        }
        public static WebElement year(WebDriver driver)
        {
            driver.findElement(By.xpath("//select[@name='birthday_year']"));
            return element;
    
        }
        public static WebElement sex(WebDriver driver)
        {
        driver.findElement(By.xpath("(//input[@name='sex'])[2]"));
        return element;
    
        }
    
        public static WebElement signup(WebDriver driver) {
            driver.findElement(By.xpath("//button[@name='websubmit']"));
            return element;
        }
        }
    

    And this is my second class where i am using this elements

    package facebook;
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.Select;
    import org.testng.annotations.Test;
    
    import facebook.facebook;
    public class signup {
        static WebDriver driver;
        @Test
        public void login()
    
        {
            System.setProperty("webdriver.chrome.driver","C:\\Users\\5558\\eclipse-workspace\\chromedriver.exe");
            driver=new ChromeDriver();
            driver.get("https://www.facebook.com");
            driver.manage().timeouts().implicitlyWait(2000, TimeUnit.MILLISECONDS);
            driver.manage().window().maximize();
            webelement ele = new webelement();
            ele.firstname(driver).sendKeys("dixit");
    
            ele.lastname(driver).sendKeys("jani");
            ele.email(driver).sendKeys("test@gmail.com");
    
            ele.newpassword(driver).sendKeys("dixit1234");
    
            Select day = new Select(ele.day(driver));
    
            day.selectByIndex(24);
    
            Select month = new Select(ele.month(driver));
    
            month.selectByVisibleText("jul");
    
            Select year = new Select(webelement.year(driver));
            year.selectByVisibleText("1997");
            ele.sex(driver).click();
    
            ele.signup(driver).click();
            driver.close();
    
    
    
    
        }
    
    
    }
    


  • Change your code like this:-
    Your POM class-

    public class webelement {
        static WebDriver driver;
        static WebElement element;
    
    public webelement(WebDriver driver) {
            //super();
            this.driver = driver;
        }
    
    
    
            public static WebElement firstname() {
                element=driver.findElement(By.xpath("//input[@name='firstname']"));
                return element; 
            }
            public static WebElement lastname()
            {
                element=driver.findElement(By.xpath("//input[@name='lastname']"));
                return element;
            }
            public static WebElement email()
            {
                element=driver.findElement(By.xpath("//input[@name='reg_email__']"));
                return element;
            }
            public static WebElement newpassword()
            {
                element=driver.findElement(By.xpath("//input[@name='reg_passwd__']"));
                return element;
            }
            public static WebElement day()
            {
                element=driver.findElement(By.xpath("//select[@name='birthday_day']"));
                return element;
            }
    
            public static WebElement month()
            {
                element=driver.findElement(By.xpath("//select[@name='birthday_month']"));
                return element;
    
            }
            public static WebElement year()
            {
                element=driver.findElement(By.xpath("//select[@name='birthday_year']"));
                return element;
    
            }
            public static WebElement sex()
            {
                element=driver.findElement(By.xpath("(//input[@name='sex'])[2]"));
            return element;
    
            }
    
            public static WebElement signup() {
                element=driver.findElement(By.xpath("//button[@name='websubmit']"));
                return element;
            }
    
    }  
    

    Your Test class-

    public class signup {
    static WebDriver driver;
        @Test
        public void login()
    
        {
            System.setProperty("webdriver.chrome.driver","C:\\Users\\5558\\eclipse-workspace\\chromedriver.exe");
            driver=new ChromeDriver();
            driver.get("https://www.facebook.com");
            driver.manage().timeouts().implicitlyWait(2000, TimeUnit.MILLISECONDS);
            driver.manage().window().maximize();
            webelement ele = new webelement(driver);
            ele.firstname.sendKeys("dixit");
    
            ele.lastname.sendKeys("jani");
            ele.email.sendKeys("test@gmail.com");
    
            ele.newpassword.sendKeys("dixit1234");
    
            Select day = new Select(ele.day);
    
            day.selectByIndex(24);
    
            Select month = new Select(ele.month);
    
            month.selectByVisibleText("jul");
    
            Select year = new Select(webelement.year);
            year.selectByVisibleText("1997");
            ele.sex.click();
    
            ele.signup.click();
            driver.close();
    
        }  
    

    Note: Just avoid the naming convention of class and also avoid using variable with same name for different purpose.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2