0

My code :

public class Testlogin {

    WebDriver driver;

    public Testlogin(WebDriver driver) {
        this.driver=driver;
    }

    WebElement userName = driver.findElement(By.id("username"));
    WebElement Password = driver.findElement(By.id("password"));
    WebElement login = driver.findElement(By.xpath("//button"));

    public void loginpages(String user,String pass) {
        userName.sendKeys(user);
        Password.sendKeys(pass);
        login.click();
    }
}

public class Testclass {

    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver(); 
        driver.get("https://the-internet.herokuapp.com/login");
        Testlogin test = new Testlogin(driver);
        test.loginpages("tomsmith","SuperSecretPassword!");
    }
}

Getting following error:

Exception in thread "main" java.lang.NullPointerException
    at Test.Testlogin.<init>(Testlogin.java:18)
    at Test.Testclass.main(Testclass.java:14)
Neuron
  • 5,141
  • 5
  • 38
  • 59
Debasish
  • 1
  • 2
  • 1
    In the testlogin class move all of the web elements to the top of the file, but only assign them in the constructor. – Aiden Grossman Apr 10 '18 at 14:25
  • 1
    Like user iiiiiii pointed out in his answer (which should have been a comment) you shouldn't capitalise variable names. Always inform yourself about the naming conventions of a new language – Neuron Apr 10 '18 at 14:41
  • You can also visit [this helpful page](http://www.oracle.com/technetwork/java/codeconventions-135099.html) that [iiiiiii](https://stackoverflow.com/users/7801777/iiiiiii) provided in her answer to review the naming convention for java – Neuron Apr 10 '18 at 14:56
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – JeffC Apr 10 '18 at 18:01

2 Answers2

1

the driver object has to be instatiated first. e.g. move it inside the constuctor:

public Testlogin(WebDriver driver)
{
    this.driver=driver;

    WebElement userName = driver.findElement(By.id("username"));
    WebElement Password = driver.findElement(By.id("password"));
    WebElement login = driver.findElement(By.xpath("//button"));

}
Gambotic
  • 824
  • 8
  • 19
1

Make the testlogin class look like below if the driver is not yet set it will point to null and when you try to run driver.findElement(By.id("username")); and the driver is null this will not work to fix this do as Aiden Grossman said these will initialize when the driver is set

public class Testlogin {

    WebDriver driver;

    public Testlogin(WebDriver driver) {
        this.driver=driver;
        WebElement userName = driver.findElement(By.id("username"));
        WebElement Password = driver.findElement(By.id("password"));
        WebElement login = driver.findElement(By.xpath("//button"));
    }

    public void loginpages(String user,String pass) {
        userName.sendKeys(user);
        Password.sendKeys(pass);
        login.click();
    }
}
Matt
  • 3,052
  • 1
  • 17
  • 30