Josh Josh - 5 months ago 24
Java Question

Null Pointer Exception when trying to take a screenshot with selenium

I have wrote a Java Class which uses selenium webdriver to quickly pass through a website and test various functions. I have also wrote a seperate class which will be used to execute the takeScreenshot() method. Once the test hits the code which executes the screenshot method, the browser closes and the J-Unit test fails and points to the line where I am calling the takeScreenshot() method. It is a null pointer exception but I cant figure out whats going wrong.. I have read through many articles and cant find an answer. I have read all of the posts on here which identify how to take a screenshot using selenium... The code is below:

**** Screenshot Class ****

public class Screenshot {


private WebDriver webDriver;
File source;

public void takeScreenshot() {

try {
source = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source, new File ("/Users/joshuadunn/Desktop/claimsScreenShot.png"));
System.out.println("Screenshot Taken!!!!");

} catch (IOException e) {
e.printStackTrace();
}
}

}


I then create a Screenshot Object and execute its takeScreenshot() method as follows:

@Test
public void testClaimsTestToCalcNode() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("becsStartCalculator")).click();
driver.findElement(By.id("MARITAL_STATUS_false")).click();
driver.findElement(By.id("btn_next")).click();
driver.findElement(By.id("HOME_ABROAD_false")).click();

*** This is where the null pointer is ***
*******************************
screenshot.takeScreenshot();
*******************************


driver.findElement(By.id("btn_next")).click();
driver.findElement(By.id("DETAILS_STUDENT_YOU_false")).click();


I hope you understand my problem!

EDIT **** - This is not a duplicate.. I am totally aware of what a null pointer is and how it can be fixed. Obviously something in my code is coming through as null, but I dont know what... The screenshot below is the only error i get (stacktrace)

J-Unit error message

Answer

You are making a new reference for WebDriver in your Screenshot class. This WebDriver is never instantiated, thus will give you a NullPointerException. Instead you should pass the WebDriver instance as a parameter to the method.

public class Screenshot {

File source;

public void takeScreenshot(WebDriver webDriver) {

    try {
        source = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);


    FileUtils.copyFile(source, new File ("/Users/joshuadunn/Desktop/claimsScreenShot.png"));
    System.out.println("Screenshot Taken!!!!");

    } catch (IOException e) {
        e.printStackTrace();
    } 
}

}

And the test case:

@Test
public void testClaimsTestToCalcNode() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.id("becsStartCalculator")).click();
    driver.findElement(By.id("MARITAL_STATUS_false")).click();
    driver.findElement(By.id("btn_next")).click();
    driver.findElement(By.id("HOME_ABROAD_false")).click();

    *** This is where the null pointer is ***
    *******************************
    screenshot.takeScreenshot(driver);
    *******************************


    driver.findElement(By.id("btn_next")).click();
    driver.findElement(By.id("DETAILS_STUDENT_YOU_false")).click();

Edit: Alternatively you could set the WebDriver in the constructor of Screenshot

public void Screenshot(WebDriver webDriver){
    this.webDriver = webDriver;
}
Comments