Null Pointer Exception when trying to take a screenshot with selenium
<p>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:</p>

<p>**** Screenshot Class ****</p>

<pre><code> 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();
    } 
}

}
</code></pre>

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

<pre><code>@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();
</code></pre>

<p>I hope you understand my problem!</p>

<p>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)</p>

<p><a href="http://i.stack.imgur.com/azPPV.png" rel="nofollow">J-Unit error message</a></p>