-2

" Here is the code of selenium web driver in which i use POM design to define all the locators of the web page. So after running the code i got an error regarding the null pointer exception.

     "
 {
  package Pages;
              import org.openqa.selenium.By;
              import org.openqa.selenium.WebDriver;
              public class LoginPage {
              WebDriver driver;
              By username = By.id("user_login");
              By password = By.id("user_pass");
              By login = By.id("wp-submit");
              By rememberme = By.id("rememberme");
              public LoginPage(WebDriver driver){
                this.driver = driver;
              }
              public void typeusername(){
                driver.findElement(username).sendKeys("admin"); 
              }
              public void typepassword(){
               driver.findElement(password).sendKeys("demo123");
              }
              public void clickrememberme(){
               driver.findElement(rememberme).click();
              }
              public void clicklogin(){
               driver.findElement(login).click();
              }
              }

"Here in this code i created an object of a login page and call all the locators using TestNG annotations."

{
 package Test;
                import org.openqa.selenium.WebDriver;
                import org.openqa.selenium.chrome.ChromeDriver;
                import org.testng.annotations.BeforeTest;
                import org.testng.annotations.Test;
                import Pages.LoginPage;
                public class verifylogin {  
                @BeforeTest
                public void beforetest(){
                    System.setProperty("webdriver.chrome.driver","C:\\Users\\saniket.soni\\Desktop\\chrome\\chromedriver.exe");
                    WebDriver driver = new ChromeDriver();
                    driver.manage().window().maximize();
                    driver.get("http://demosite.center/wordpress/wp-login.php");
                }
                @Test   
                public void verifyvalidlogin(){
                    WebDriver driver = null;
                    LoginPage login_test = new LoginPage(driver);
                    login_test.typeusername();
                    login_test.typepassword();
                    login_test.clickrememberme();
                    login_test.clicklogin();    
                }
                }
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – JeffC Mar 27 '17 at 20:02

4 Answers4

1

You might want to consider studying scope in java to understand what is happening in this issue. Here is an explanation for what is happening in your case.

In your public void beforetest() method, you're creating a driver, and using it, but when that method is done the driver variable goes out of scope, and you no longer have access to it.

Later, in your verifyValidLogin method, you're creating a new driver variable, and setting it to null:

WebDriver driver = null;

Then you pass this null driver to your LoginPage here:

LoginPage login_test = new LoginPage(driver);//driver is null here

So when you use it here:

public LoginPage(WebDriver driver){
    this.driver = driver;
}
public void typeusername(){
    driver.findElement(username).sendKeys("admin"); 
}

It's null

Try making these changes:

First, save your driver somewhere when you initialize it like this:

public class verifylogin {

    //STORE YOUR DRIVER SOMEWHERE!!! Like here
    private WebDriver driver;  

    @BeforeTest
    public void beforetest(){
        System.setProperty("webdriver.chrome.driver","C:\\Users\\saniket.soni\\Desktop\\chrome\\chromedriver.exe");
        //Store the driver in the class variable
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("http://demosite.center/wordpress/wp-login.php");
    }

    @Test   
    public void verifyvalidlogin(){
        //Now you can use the driver you stored
        LoginPage login_test = new LoginPage(driver);
        login_test.typeusername();
        login_test.typepassword();
        login_test.clickrememberme();
        login_test.clicklogin();    
    }
mrfreester
  • 1,981
  • 2
  • 17
  • 36
0

You are removing object reference by using "WebDriver driver = null" in verifyvalidlogin() method. Remove this line and try, it will work.

0

You instance variable 'driver' as private ('private WebDriver driver').

With 'private' access modifier you can access driver within the class but not from outside, change it to public it will work.

0

In beforetest() method you are instantiating the browser object and navigating to demo Url.

After that in verifyvalidlogin() method you are making driver object to null and passing this null object to Login page, here you are passing null instead of driver object.

We can solve this issue by declaring driver instance variable inside the class and outside of all methods like below.

WebDriver driver;

Instantiate this driver object in beforetest() method and pass the same driver object to LoginPage without declaring it again as mentioned below.(I executed this script in my local machine its working fine)

package Tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import Pages.LoginPage;
public class verifylogin {  

WebDriver driver;

@BeforeTest
public void beforetest(){
        System.setProperty("webdriver.chrome.driver","C:\\Users\\saniket.soni\\Desktop\\ chrome\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("http://demosite.center/wordpress/wp-login.php");
}
@Test   
public void verifyvalidlogin(){
    LoginPage login_test = new LoginPage(driver);
    login_test.typeusername();
    login_test.typepassword();
    login_test.clickrememberme();
    login_test.clicklogin();    
}
}

package Pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class LoginPage {

WebDriver driver;
By username = By.id("user_login");
By password = By.id("user_pass");
By login = By.id("wp-submit");
By rememberme = By.id("rememberme");
public LoginPage(WebDriver driver){
    this.driver = driver;
}
public void typeusername(){
    driver.findElement(username).sendKeys("admin"); 
}
public void typepassword(){
    driver.findElement(password).sendKeys("demo123");
}
public void clickrememberme(){
    driver.findElement(rememberme).click();
}
public void clicklogin(){
    driver.findElement(login).click();
}

}

Try this solution and let me know if it works for you.

Akarsh
  • 967
  • 5
  • 9