Skip to content
Advertisement

Code in “catch” block not executing

please help me understand why:) My program reach the “try” line, but seems to skip the “catch”, Although its printing the stacktrace… I’m using JOptionPane in my catch, but also System.out.println() not working. Code:

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;


public class main {

    public static void main(String[] args) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
        Calendar cal = Calendar.getInstance();
        String fileName = dateFormat.format(cal.getTime());
        Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        BufferedImage bufferedImage;
        try {
            bufferedImage = new Robot().createScreenCapture(rectangle);
            try
            {
                ImageIO.write(bufferedImage, "jpg", new File("c:/temp/ScrenShots/"+fileName+".jpg"));
            } catch (IOException e)
            {
                JOptionPane frame = null;
                JOptionPane.showMessageDialog(frame,"Failed to take screen-shot", "ScreenShots", JOptionPane.ERROR_MESSAGE);
                e.printStackTrace();
            }
        } catch (AWTException e) {
            JOptionPane frame = null;
            JOptionPane.showMessageDialog(frame,"AWT Error", "ScreenShots", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }

    }

}

Exception:

java.io.FileNotFoundException: c:tempScrenShots20.10.2014.jpg (The system cannot find the path specified)
    at java.io.RandomAccessFile.open(Native Method)
    at java.io.RandomAccessFile.<init>(Unknown Source)
    at javax.imageio.stream.FileImageOutputStream.<init>(Unknown Source)
    at com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstance(Unknown Source)
    at javax.imageio.ImageIO.createImageOutputStream(Unknown Source)
    at javax.imageio.ImageIO.write(Unknown Source)
    at main.main(main.java:26)
Exception in thread "main" java.lang.NullPointerException
    at javax.imageio.ImageIO.write(Unknown Source)
    at main.main(main.java:26)

Advertisement

Answer

There is an NullPointerException internally just before the FileNotFoundException

From your log.

   Exception in thread "main" java.lang.NullPointerException //*
   at javax.imageio.ImageIO.write(Unknown Source)
   at main.main(main.java:26)

You can make sure it from following. This code here only for point out the issue. catching NullPointerException is a bad practice in coding.

 try {
    // current code
 } catch (IOException e) {
   // current code
 }catch (NullPointerException e){
  JOptionPane frame = null;
  JOptionPane.showMessageDialog(frame, "NPE", "npe", JOptionPane.ERROR_MESSAGE);
 }
Advertisement