I am currently setting up PageObject with PageFactory Automation tests. I currently have a screenCapture.java and I try to call the method within the screenCapture when there's an error.

Here is the code from screenCapture:

    public void screenshotOnError() throws IOException {
    long epoch = System.currentTimeMillis();
    String ssTimestamp = String.valueOf(epoch);

    File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(scrFile, new File("/Users/sqadan/Desktop/workFiles/maven/MavenProject/screenshots/" + ssTimestamp + ".jpg"));

and here's the code that's calling the screenshotOnError method:

    try {

        logs.InfoDetails("Login successful");
    }catch (NoSuchElementException e) {
        logs.errorDetails("Login unsuccessful");

I currently have this test to purposely fail, but when it does, I get a NPE exception:

at com.acton.util.screenCapture.screenshotOnError(screenCapture.java:24)
at com.acton.team.inbound.actonLogin.doLogin(actonLogin.java:46)
  • Where comes the driver in screenCapture from? My assumption is, that the driver variable is null. Suggestion: Add a driver parameter to the screenshotOnError method and try it again. If it works, in your current solution are the drivers in your test case and in the screenCapture method not equals.
    – Twaldigas
    May 12, 2015 at 10:02
  • Your exception is line 24 in screenCapture, but which line in your posted code is line 24?
    – Julian
    Aug 11, 2015 at 23:41

1 Answer 1


NullPointerException (NPE) can occur in some situations. To prevent them, try to avoid these situations:

  1. Your browser should be open in screen. You can not minimize while taking screenshot. This is a known limitation - screenshot captures only work with opened/window display.

  2. Remove turn off display option from sleep mode. If your test system has display off while in sleep mode, you will receive a null pointer exception because there is no display to take a screenshot of.

  3. This scenario is very rare: If you have dual screens and you are working in primary window while running your test case in the secondary screen, you will get a null pointer exception. It's best to run your test cases in the primary screen.

Once you've eliminated these possible causes, if you're still getting null pointer exceptions taking screenshots, you could try these links to see if they help you:

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.