-1

I am getting nullpointexception on login page and in actual step definitions

I have a base page :

package pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org
.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

public class basePage {
    protected WebDriver driver;
@BeforeClass
public void setupApplication() 
{
             System.setProperty("webdriver.chrome.driver", "C:/DRIVERS/Selenium Drivers/chromedriver.exe");
             WebDriver driver = new ChromeDriver();
             driver.manage().window().maximize();
             driver.get("http://salesforce.com");
          }

@AfterClass
public void closeApplication()
{
    driver.quit();

}
}

Then I have a login page :

package pages;
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class LogInPage extends basePage {
    private static WebElement element = null;
      public static WebElement txtbx_UserName(WebDriver driver){
            element = driver.findElement(By.id("username"));
               return element;
             }

       public static WebElement txtbx_Password(WebDriver driver){
             element = driver.findElement(By.id("password"));
               return element;
                    }
       public static WebElement btn_LogIn(WebDriver driver){
              element = driver.findElement(By.id("Login"));
                    return element;
                    }
            }

And I am calling these from step definitions file:

package Step_Defnitions;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import cucumber.api.DataTable;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import pages.LogInPage;
import pages.MyDayAppTabsManager;
import pages.basePage;

public class SalesforceLogin{

    private WebDriver driver;

@Given("^User login to Salesforce application$")
     public void user_login_to_Salesforce_application(DataTable userCredentials) throws Throwable {

 //Write the code to handle data table
   List<List<String>> data = userCredentials.raw();
   LogInPage.txtbx_UserName(driver).sendKeys(data.get(0).get(0));
   LogInPage.txtbx_Password(driver).sendKeys(data.get(0).get(1));
   LogInPage.btn_LogIn(driver).click();
    }
}

I am getting nullpointerexception error at LogInPage.txtbx_UserName(driver).sendKeys(data.get(0).get(0)); and also in LogInPage at element = driver.findElement(By.id("username")); .I am doing something wrong in initializing the driver i think, any thoughts.

Thanks.

  • 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 May 02 '17 at 18:39
  • 1
    You should spend some time learning how to debug your own programs. Once you learn this crucial skill, you will be able to solve problems like this easily by just stepping through your own code. – JeffC May 02 '17 at 18:40

1 Answers1

1

Change this line WebDriver driver = new ChromeDriver();

To driver = new ChromeDriver();

Kushal Bhalaik
  • 3,349
  • 5
  • 23
  • 46