java.lang.IllegalArgumentException:im == null!错误

时间:2012-01-05 05:44:48

标签: java multithreading exception

我刚刚学会了如何使用线程,我正在尝试使用线程编码一些图像,但我会收到此错误 java.lang.IllegalArgumentException:im == null!
我尝试没有线程做同样的事情,它只是慢慢地工作,不是因为图像io,而是因为编码图像所需的时间。  WinDef.HDC是我从JNA库获得的变量类型。当我没有线程尝试它时,我可以很容易地写出1000张图像(约60mb)。

//The thread class
public class imageEncoderThread extends Thread {

   HDC originalImage;
    BufferedImage image;


    public imageProcessThread (HDC rI) {
       originalImage = rI'
    }

    @Override
    public void run() {
        //returns a buffered image
        image = process(rawImage);
        try {
            ImageIO.write(image, "jpg", new File (filePath));
        } catch (IOException e) {

        }

    }

}


 //full stack trace 
    Exception in thread "Thread-5" java.lang.IllegalArgumentException: im == null!
        at javax.imageio.ImageIO.write(Unknown Source)
        at javax.imageio.ImageIO.write(Unknown Source)
        at com.capture.SingleImageEncoderThread.run(Thread.java:33)
    Exception in thread "Thread-7" java.lang.IllegalArgumentException: im == null!
        at javax.imageio.ImageIO.write(Unknown Source)
        at javax.imageio.ImageIO.write(Unknown Source)
        at com.capture.SingleImageEncoderThread.run(Thread.java:33)
    Exception in thread "Thread-6" java.lang.IllegalArgumentException: im == null!
        at javax.imageio.ImageIO.write(Unknown Source)
        at javax.imageio.ImageIO.write(Unknown Source)

 //and so on with the same exception 

1 个答案:

答案 0 :(得分:3)

几乎可以肯定你不能打电话:

image = JNAScreenShot.encodeRawWindow(rawImage, bounds);

来自主ui线程以外的任何线程。看到: http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6398cda5-5393-4b16-b8dc-5bbf8902033f/(具体来说,我认为HDC不能在主ui线程之外使用)。

因此,您需要先创建屏幕截图,然后将其传递给您的线程以执行IO(无论如何,这可能是缓慢的部分)。