'Unable to save the screenshot in Selenium using Robot class

When I try to take screenshot using the Robot Class. I'm getting

javax.imageio.IIOException: Can't create an ImageOutputStream!

error:

Code Snippet:

    public  static void  takeScreenshot() throws AWTException, IOException {
    DateFormat dateFormatFolder=new SimpleDateFormat("DD-MM-YYYY");
    DateFormat dateFormatFile=new SimpleDateFormat("DD-MM-YYYY hh-mm-ss");
    Date date=new Date();
    
    Robot robot=new Robot();
    Dimension screenDimension=Toolkit.getDefaultToolkit().getScreenSize();
    Rectangle rectangle=new Rectangle(screenDimension);
    BufferedImage sourceImage=robot.createScreenCapture(rectangle);
    File dest=new File("C:\\Users\\surendraanand.ramas\\eclipse-workspace (Local)\\CaseStudy\\Output\\"+dateFormatFolder.format(date)+"\\"+"Screesnhot - "+dateFormatFile.format(date)+".jpeg");
    
    ImageIO.write(sourceImage, "jpeg", dest);
    
}

Note: if I remove the dateFormatFolder.format(date) it works. Not sure what I'm missing.

javax.imageio.IIOException: Can't create an ImageOutputStream!
    at java.desktop/javax.imageio.ImageIO.write(ImageIO.java:1553)
    at com.dummy.NewTest.mainTest(NewTest.java:49)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.testng.TestRunner.privateRun(TestRunner.java:794)
    at org.testng.TestRunner.run(TestRunner.java:596)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
    at org.testng.SuiteRunner.run(SuiteRunner.java:276)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
    at org.testng.TestNG.runSuites(TestNG.java:1063)
    at org.testng.TestNG.run(TestNG.java:1031)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source