Search code examples
javaseleniumautomated-testspageobjects

Selenium POM Model: java.lang.NullPointerException Error. Not Able To Enter Values For Webelements Or Select Elements


I am implementing POM model sleneium for the first time.

I am using skeleton function to initialize my WebDriver. It is as below:

File pathToBinary = new File("<path>\firefox.exe");
FirefoxBinary binary = new FirefoxBinary(pathToBinary);     
FirefoxProfile profile = new FirefoxProfile();  
profile.setPreference("network.proxy.http", "<proxyaddress>");
profile.setPreference("network.proxy.http_port", "<portnumber>");
driver = new FirefoxDriver(binary, new FirefoxProfile());
driver.manage().window().maximize();

My application is staring up properly and I able to login. But when I traverse to a drifferent link I receive java.lang.NullPointerException.

I have deduced the issue as it is being caused because of all objects of class (of different pages) are being initialized during the lauch of selenium suite and hence my elements class being executed at second or third position have not been initialized.

First line in below code is being used for getting instance of driver and remaining code is used fo rcreating objects.

WebDriver driver = Driver.AppDriver.getInstance();
FirstClass obFirstClass  = new FirstClass();
SecondClass objSecondClass  = new SecondClass ();   
ThirdClass objThirdClass  = new ThirdClass ();

using objFirstClass, I am able to login to my system and verify my login too. using objSecondClass, I can print string to represent success of login. but using objThirdClass, I am not able to enter values for WebElements or Select objects.

It gives Null Exception error.

public class TestClass 
{
    WebDriver driver = Driver.AppDriver.getInstance();

    FirstClass obFirstClass  = new FirstClass();
    SecondClass objSecondClass  = new SecondClass ();   
    ThirdClass objThirdClass  = new ThirdClass (driver);

    @Test(priority=2)
    public void method()
        {   
            objThirdClass.action1();
            System.out.println("after action"); //-> This line is being printed

            objThirdClass.action2(param1, param2, param3);
        }           
}

public class ThirdClass {

    WebDriver driver = Driver.AppDriver.getInstance();

    public ThirdClass(WebDriver _driver){         
        //This initElements method will create all WebElements 
        driver = _driver;
        PageFactory.initElements(driver, this); 
    }

    @FindBy(xpath=<xpath>)
    WebElement elementCreate;

    @FindBy(id=<id1>)
    Select selectElement1;

    @FindBy(id=<id2>)
    Select selectElement2;

    @FindBy(id=<id3>)
    Select selectElement3;

    @FindBy(id="submit")
    WebElement elementSubmit;

    public void action1()
        {
            JavascriptExecutor executor2 = (JavascriptExecutor)driver;
            executor2.executeScript("arguments[0].click();", elementCreate);
            System.out.println("Create link found");
        }

    public void setElement1(String str1)
        {
            selectElement1.selectByVisibleText(str1);
        }

    public void setElement1(String str2)
        {
            selectElement2.selectByVisibleText(str2);
        }

    public void setElement1(String str3)
        {
            selectElement3.selectByVisibleText(str3);
        }


    public void submit()
        {
            submit.click();
        }


    public void action2(String string1, String string2, String string3,)
        {               
            this.setElement1(str1);
            this.setElement2(str2);
            this.setElement3( str3) 
            this.submit();  
        }   
}

Solution

  • I referred to below link:

    selenium webdriver select element

    Select(WebElement element)
    

    So if you do something like this:

    @FindBy(id="foo")
    private WebElement wannabeSelect;
    Select realSelect = new Select(wannabeSelect);
    realSelect.selectByValue("myValue");
    

    It should work.

    BTW, I am using the same approach as you in the "workaround" because I dont wanna cast new WebElement object when I need Select object. But anyways, the

    sDriver.findElement(By.id("foo"));
    returns WebElement, so thats why its working. You can also do this:
    
     WebElement wannabeSelect = sDriver.findElement(By.id("foo"));
     Select foo = new Select(wannabeSelect);
    

    It resolved issue.