user4090 user4090 - 4 months ago 34
Java Question

java.lang.IllegalArgumentException: im == null! error

I just learned how to use threads yesterday and I'm trying to encode some images using threads but I'll get this error
java.lang.IllegalArgumentException: im == null!

I tried doing this exact same thing without threads and it worked fine just kind of slowly, not because of the image io but because of the time it takes to encode images.
the WinDef.HDC is a variable type I got from the JNA library. When I did try it without threads I could easily write out 1000 images (about 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

Answer

It's almost certainly the case that you cannot call:

image = JNAScreenShot.encodeRawWindow(rawImage, bounds);

from any thread other than the main ui thread. See: http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/6398cda5-5393-4b16-b8dc-5bbf8902033f/ (specifically, I do not think HDCs are usable outside the main ui thread).

So, you'll need to create the screenshot first, then pass it to your thread to do the IO (that's probably the slow part, anyway).