2014-10-20 54 views
-1

请帮我理解为什么:)我的程序到达“try”行,但似乎跳过了“catch”,虽然它打印栈跟踪... 我正在使用JOptionPane在我的catch中,也是System.out.println()不起作用。 代码:代码中“catch”块不执行

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

    } 

} 

例外:

java.io.FileNotFoundException: c:\temp\ScrenShots\20.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) 
+0

这是内在的,因为FileNotFoundException是一个IOException – triggerNZ 2014-10-20 08:09:20

+0

看起来你没有截图目录。 – eckes 2015-01-26 22:36:40

+0

看起来你没有截图目录。我不认为你可以显示没有主框架的对话框。 – eckes 2015-01-26 22:38:06

回答

2

有内部的NullPointerException只是FileNotFoundException

从你的日志之前。

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

你可以从下面确定它。这里的代码只是为了指出问题。捕获NullPointerException在编码方面是不好的做法。

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

好猜! FileNotFound首先出现在“新文件(”c:/ temp/ScrenShots /“+ fileName +”。jpg“)”中。然后NullPointerException发生在“ImageIO.write(bufferedImage,”jpg“,Null))”其中Null来自“新文件(”c:/ temp/ScrenShots /“+ fileName +”。jpg“)”。 – 2014-10-20 08:22:32

+0

@ Fumu7很好的解释,谢谢! – Erez 2014-10-20 08:28:29