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)
4
  • 1
    In the testlogin class move all of the web elements to the top of the file, but only assign them in the constructor. Commented Apr 10, 2018 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 Commented Apr 10, 2018 at 14:41
  • You can also visit this helpful page that iiiiiii provided in her answer to review the naming convention for java Commented Apr 10, 2018 at 14:56
  • Possible duplicate of What is a NullPointerException, and how do I fix it? Commented Apr 10, 2018 at 18:01

2 Answers 2

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"));

}
Sign up to request clarification or add additional context in comments.

2 Comments

You beat me by seconds nice
wlc - then you should accept the answer (stackoverflow.com/help/accepted-answer)
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();
    }
}

1 Comment

you wrote more than me ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.